蟒蛇。从覆盖的属性

时间:2017-07-21 08:17:16

标签: python overwrite

我正在尝试覆盖第三方的Button类属性,但我面临一个奇怪的问题。

以下是覆盖“Pressed”属性的代码

class Button(Button):

    def __init__(self):
        super().__init__()        

    @property
    def Pressed(self):
        print("Do something")        
        return super(Button,self).Pressed

    @Pressed.setter
    def Pressed(self,func):
        print("Pressed property {}".format(super(Button,self).Pressed))
        print(hasattr(super(Button,self),"Pressed"))
        setattr(super(Button,self),'Pressed',func)

当调用“setter”时,它会打印:

  • 按下属性无
  • 然后抛出AttributeError:'super'对象没有属性'Pressed'

但是,如果我检查前一行代码,如果它有属性并返回“True”,怎么可能在下一行抛出一个错误,说没有attribe“按下”??? ?

我也试过super(Button,self) = func,但结果相同。

PS:我无法访问Button超类代码。

0 个答案:

没有答案