繁体   English   中英

@property AttributeError: 'bool' object 没有属性 'setter' - python

[英]@property AttributeError: 'bool' object has no attribute 'setter' - python

我有一个像这样的脚本

w = False

class A(object):
    @property
    def is_w(self):
        global w
        return w

    @w.setter
    def is_w(self, value):
        global w
        w = value
        self.listen(value)

    def listen(self,w_):
        if w_:
            print("worked!")
            w = False
   def value_change(self):
        global w
        w = True

我需要 listen() function 在 w 值变为真时运行,并在它再次为假时停止。 w 值在另一个 function 更改时更改,例如这里 value_change() function 但是我收到此错误AttributeError: 'bool' object has no attribute 'setter'

@property将应用它的 function 转换为属性 object。 该 object 具有您需要的setter属性。 所以setter需要用<function with @property>.setter

class A(object):
    @property
    def is_w(self):
        global w
        return w

    @is_w.setter  # here
    def set_w(self, value):  # rename this method to a different name
        global w
        w = value
        self.listen(value)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM