I was reading property(), which I understand is attribute access goes through the method specified in property(). But I got "RuntimeError: maximum recursion depth exceeded", when executed the following code.
class Property(object):
def __init__(self):
self.x = "Raj"
def gettx(self):
print "getting x"
return self.x
def settx(self, val):
print "Setting x"
self.x = val
def dellx(self):
print "deleting"
return self.x
x = property(gettx, settx, dellx, "I'm object property")
p = Property()
print "p.x", p.x
p.x = "R"
print "p.x:", p.x
Is it not possible to apply property in this way. Because it worked fine when 'self.x' changed to self._x and self.__x.
The “maximum recursion depth exceeded in comparison” error is raised when you try to execute a function that exceeds Python's built in recursion limit. You can fix this error by rewriting your program to use an iterative approach or by increasing the recursion limit in Python.
The recursion depth limit in Python is by default 1000 . You can change it using sys. setrecursionlimit() function.
Due to this, the recursion limit of python is usually set to a small value (approx, 10^4). This means that when you provide a large input to the recursive function, you will get an error. This is done to avoid a stack overflow. The Python interpreter limits the recursion limit so that infinite recursions are avoided.
The error is due to the following infinite recursion loop: you have defined a property x
with uses the gettx
, settx
and deltx
access methods, but the access methods themselves try to access the property x
(i.e. call themselves).
You should write the code along the following lines:
class Property(object):
def __init__(self):
self.__x = "Raj" # Class private
def gettx(self):
print "getting x"
return self.__x
def settx(self, val):
print "Setting x"
self.__x = val
def dellx(self):
print "deleting"
return self.__x
x = property(gettx, settx, dellx, "I'm object property")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With