RecursionError When Using @property Decorator
I am learning the @property manipulation and writing some codes as below, while the cmd just shows, Traceback (most recent call last): File 'C:\Users\mckf1\pyfile\new.py', line 23,
Solution 1:
Decorators are not ignored when accessing the property from within the class. So when the width()
method does
return self.width
that invokes the width()
method again, which tries to return self.width
, which invokes the method, and so on and so one.
This is why you need to use different names for the properties internally to the class than the names of the decorator methods.
@property
def width(self):
return self._width
Accessing the _width
property doesn't try to use the decorated method, so you don't get into an infinite recursion.
Post a Comment for "RecursionError When Using @property Decorator"