James James - 1 year ago 43
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

def get_lat(self):
return self._latitude

def get_long(self):
return self._longitude

def get_timestamp(self):
return self._timestamp

def get_filename(self):
return self._filename

def get_compas(self):
return self._compas

def is_duplicate(self):
return self._duplicate

def is_teleporting(self):
return self._teleporting

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

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

Answer Source


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...