James James - 4 months ago 10
Python Question

Maximum recursion depth on class

I have a class that I'm trying to set is_duplicate to True like:

file = FileProperties(long, lat, timestamp, compas, filename)
[...]
file.is_duplicate = True


And I get a RuntimeError: maximum recursion depth exceeded while calling a Python object what exactly am I doing wrong? The file class creation is also happening in a for loop, but I don't think that's the issue with this specific error.

class FileProperties(object):
def __init__(self, longitude, latitude, timestamp, compas, filename):
self._longitude = longitude
self._latitude = latitude
self._timestamp = timestamp
self._filename = filename
self._compas = compas
self._duplicate = False
self._teleporting = False

@property
def get_lat(self):
return self._latitude

@property
def get_long(self):
return self._longitude

@property
def get_timestamp(self):
return self._timestamp

@property
def get_filename(self):
return self._filename

@property
def get_compas(self):
return self._compas

@property
def is_duplicate(self):
return self._duplicate

@property
def is_teleporting(self):
return self._teleporting

@is_duplicate.setter
def is_duplicate(self, value):
self.is_duplicate = value

@is_teleporting.setter
def is_teleporting(self, value):
self._teleporting = value

Answer

In:

@is_duplicate.setter
def is_duplicate(self, value):
    self.is_duplicate = value

Change self.is_duplicate to self._duplicate and it should work I guess (else please provide a minimal working example).

The reason for this bug is that you are assigning the method is_duplicate instead of the attribute _duplicate hence you are creating an infinite call loop, because the method is trying to set itself in an infinite loop because it goes through the setter again and again and again...

Comments