Why does trying to set "points" using Python properties cause infinite recursion?

Why does trying to set "points" through Python properties cause infinite recursion?

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

      


When it tries to plot it with a value of -5 (which should set it to 0, via a property), it recurses infinitely on the line self.points = points

in the setter / tec function@points.setter.

Thank!

+3


source to share


2 answers


Because the self.points = ...

call to setter; inside the setter self.points = ...

, the calling device is called; recursion is repeated until.

By using a different name, you can prevent recursion: self._points

eg.



Or self.points = ...

use instead self.__dict__['points'] = ..

(same for getter):

@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)

      

+4


source


This is because inside yours property setter

it calls itself again:

@points.setter
def points(self, points):
    if (points < 0):
        self.points = 0 # call itself again here
    else:
        self.points = points # call itself again here

      



You need a different field to store the actual value when used property

, and it would be better to be "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

      

+1


source







All Articles