Mo .haytham
Mo .haytham

Reputation: 291

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

I have a script that goes like this

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

I need the listen() function to run whenever the w value becomes true and stops when it's false again. the w value changes when another function changes it for example here the value_change() function However I get this error AttributeError: 'bool' object has no attribute 'setter'

Upvotes: 0

Views: 1190

Answers (1)

rdas
rdas

Reputation: 21275

@property transforms the function that it's applied on into a property object. That object has the setter attribute that you need. So the setter needs to be annotated with <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)

Upvotes: 0

Related Questions