@a.setter
def a(self, value):
print ("called a setter")
self.a = value
When self.a = value
executes, it calls your method a(self, value)
again, which executes self.a = value
again, which calls a(self, value)
... etc.
The conventional solution is to have different names for the property and the underlying attribute. Ex. you can add an underscore to the front.
class A:
def __init__(self):
self._a = 0
@property
def a(self):
print ("called a getter")
return self._a
@a.setter
def a(self, value):
print ("called a setter")
self._a = value
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…