Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

Here is a test class that I wrote to became familiar with @properties and setter functionality in Python script:

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

The problems is that when I want to create an object from my class, I face the following error:

>>> t = Test(1)  Traceback (most recent call last):   File "<pyshell#19>", line 1, in <module>     t = Test(1)   File "<pyshell#18>", line 3, in __init__     self.x =  value   File "<pyshell#18>", line 9, in x     self.x = value   File "<pyshell#18>", line 9, in x   #A bunch of lines skipped RuntimeError: maximum recursion depth exceeded >>>  
like image 602
EbraHim Avatar asked Apr 29 '16 06:04

EbraHim


People also ask

How do I fix maximum recursion depth exceeded in comparison Python?

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.

How do you overcome maximum recursion depth in Python?

Conclusion. The recursion depth limit in Python is by default 1000 . You can change it using sys. setrecursionlimit() function.

What is the maximum depth of recursion function in Python?

The recursion limit is usually 1000.

How do you find recursion depth in Python?

To get the current value of the recursion limit in Python, we will import sys module, and then we will use “sys. getrecursionlimit()” to get the current recursion limit.


1 Answers

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 
like image 191
Reblochon Masque Avatar answered Sep 28 '22 10:09

Reblochon Masque