简体   繁体   English

类上的最大递归深度

[英]Maximum recursion depth on class

I have a class that I'm trying to set is_duplicate to True like: 我有一个类,我试图将is_duplicate设置为True,如:

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? 我得到一个RuntimeError:在调用Python对象时超出了最大递归深度我究竟做错了什么? The file class creation is also happening in a for loop, but I don't think that's the issue with this specific error. 文件类创建也发生在for循环中,但我不认为这是特定错误的问题。

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

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). 将self.is_duplicate更改为self._duplicate并且它应该可以正常工作(否则请提供最小的工作示例)。

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... 这个错误的原因是你正在分配方法is_duplicate而不是属性_duplicate,因此你创建了一个无限的调用循环,因为该方法试图将自己设置为无限循环,因为它一次又一次地通过setter ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM