为什么通过Python属性设置“points”会导致无限递归?

3

为什么尝试通过Python属性设置“points”会导致无限递归?

使用Python 3

import Task

myTask = Task.Task("Test",-5)

myTask.points = -7

print(myTask)

class Task:

def __init__(self,name="",points=0,times_done=0):
    self.name = name
    self.points = points
    self.times_done = times_done

@property
def points(self):
    return self.points

@points.setter
def points(self, points):
    if (points < 0):
        self.points = 0
    else:
        self.points = points

def __str__(self):
    return "The task '" + self.name + "' is worth " + str(self.points) + " and has been completed " + str(self.times_done) + " times."

当使用值-5构造它(通过属性应该将其设置为0)时,它在setter函数/装饰器@points.setter中的行self.points = points上无限递归。

谢谢!


1
我无法重现这个错误。你在这里省略了任何代码吗? - Julien
只有那些完全不相关的东西。我会进行编辑。 - Matt
1
@Julien,OP 应该使用 Python 3.x。如果您正在使用 Python 2.x,则可以通过继承 object 来复制:class Task(object) - falsetru
我正在使用 Python 3。 - Matt
2个回答

4

因为self.points = ...调用了setter方法,在setter方法内部,执行了self.points = ...,从而再次调用了setter方法,导致递归重复直到堆栈溢出。

通过使用其他名称,可以防止递归:例如self._points

或者,可以使用self.__dict__['points'] = ..(getter方法同理)来替代self.points = ...

@property
def points(self):
    return self.__dict__['points']

@points.setter
def points(self, points):
    if points < 0:
        self.__dict__['points'] = 0
    else:
        self.__dict__['points'] = points
    # self.__dict__['points'] = max(0, points)

1
那是因为在你的属性设置器内部,它再次调用了自身:
@points.setter
def points(self, points):
    if (points < 0):
        self.points = 0 # call itself again here
    else:
        self.points = points # call itself again here

当您使用property时,需要另一个字段来存储实际值,最好是一个"private" field
class Task(object):
    def __init__(self,name="",points=0,times_done=0):
            self.name = name
            self.points = points
            self.times_done = times_done

    @property
    def points(self):
            return self._points

    @points.setter
    def points(self, points):
            if (points < 0):
                    self._points = 0
            else:
                    self._points = points

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接