Using Properties in Python classes cause "maximum recursion depth exceeded" [duplicate] Using Properties in Python classes cause "maximum recursion depth exceeded" [duplicate] python-3.x python-3.x

Using Properties in Python classes cause "maximum recursion depth exceeded" [duplicate]


You are using the same name for the getter, setter and attribute. When setting up a property, you must rename the attribute locally; the convention is to prefix it with an underscore.

class Test(object):    def __init__(self, value):        self._x =  value    @property    def x(self):        return self._x


The problem is in these lines:

def x(self):    return self.x

Replace it with

def get_x(self):    return self.x

Because now the function calls itself, leading to the recursion depth exceeded.