From an old example, I tried to insert a counter member to the class Point but when I run, it won't evaluate with Error: RecursionError: maximum recursion depth exceeded!. However, I didn't have such impression this error happened years ago with earlier Python version.
EDIT: the line id=0 was an mistaken.
import math
class Point():
# id=0
def __init__(self, x, y, z:float=0):
import sys; print(f"{self.id=}>{sys.getrecursionlimit()}")# maximum recursion depth 1000 exceeded
self._x = x; self._y = y; self._z = z; self.id+=1
@property
def x(self):
return self._x
@x.setter# to define a "non-public member"
def x(self, value):
self._x=value
@property
def y(self):
return self._y
@y.setter
def y(self, value):
self._y=value
@property
def xy(self):
return [self._x,self._y]
@xy.setter
def xy(self,xval,yval):
self._x=xval;self._y=yval
@property
def z(self):
return self._z
@z.setter
def z(self, value):
self._z=value
@property
def id(self):
return self.id
# @id.setter
# def id(self, value): # raise AttributeError("Can't")
# raise WriteCoordinateError("Point Number id is read-only")
@property
def distance(self):
return round(math.dist((0, 0, 0), (self._x, self._y, self._z)))
As JonSG said, this is because you have mutiple definitions of id. On top of your class, you say: id=0
. But at the end, you define the property id (which can now be referred as self.id
) as a function that returns self.id
. because self.id
is a property again, a function is called named self.id()
, which returns itself.
hopefully it is now clear to you why this happens, but I will give one more example:
def f(x):
return f(x)
this is sort of what your function does.
you could name the private id
to _id
and return self._id
.
your code becomes than:
class Point() # These brackets are redunant, do you want to put something in later?
_id = 0
...
...
@property
def id(self):
return self._id