小编rvc*_*and的帖子

如何使用 xlwings 调整宽度列

我编写了一个函数,用xlwings将数据保存在 WorkBook 中,我想适应宽度列

class VehicleClass:
    def __init__(self, VehClass):
        # Vehicles 4 - Vehicle Classes
        self.VehClass = [VehClass]
        self.VehName = []
        self.ScaleFactor = []

    def add(self, VehName, ScaleFactor):
        self.VehClass*=1
        self.VehName.append(VehName)
        self.ScaleFactor.append(ScaleFactor)        

    def save(self):
        # Vehicles 4 - Vehicle Classes
        sht = wb.sheets['Vehicles 4 - Vehicle Classes']
        rng = sht.range("A1").end('down').offset(1, 0)

        for i, item in enumerate([self.VehClass, self.VehName, self.ScaleFactor]):
            rng.offset(0, i).options(transpose=True).value = item
            rng.autofit() # or rng.columns.autofit()
Run Code Online (Sandbox Code Playgroud)

但不起作用。

你有什么主意吗 ?谢谢 !

python excel python-3.x xlwings

8
推荐指数
2
解决办法
8666
查看次数

默认情况下__eq __()方法中的内容

我读到这里(从这里):

用户定义的类默认具有__eq__()__hash__()方法。使用它们,所有对象比较不相等(除了它们本身)并x.__hash__()返回适当的值,以使x == y暗示x为y和hash(x)== hash(y)。

我想知道__eq__()默认情况下该方法是否定义为:

def __eq__(self, other):
    return hash(self) == hash(other)
Run Code Online (Sandbox Code Playgroud)

python python-3.x

2
推荐指数
1
解决办法
64
查看次数

为什么我无法在对象的__setattr__方法中获取属性?

我有一个具有某些属性的类,在检查其他属性后可以更改该属性,因此我对此类进行了编码:

class MyClass():
    def __init__(self, x):
        self.x = x

    def __setattr__(self, name, value):
        print(self.x)  # doesn't work

        self.__dict__[name] = value

if __name__ == '__main__':
    myObj = MyClass(1)

    myObj.x = 2
Run Code Online (Sandbox Code Playgroud)

但是我得到这个错误

AttributeError: 'MyClass' object has no attribute 'x'
Run Code Online (Sandbox Code Playgroud)

如果我没有print(self.x)重写的属性,但是我需要检查其他属性以更改另一个属性。

我尝试了self.__dict__[name]getattr(self,name),但是遇到了同样的错误。

python class operator-overloading python-3.x

1
推荐指数
1
解决办法
35
查看次数