Friday, 15 February 2013

python - "maximum recursion depth exceeded" when "property" is applied to instance variable "self.x" -



python - "maximum recursion depth exceeded" when "property" is applied to instance variable "self.x" -

i reading property(), understand attribute access goes through method specified in property(). got "runtimeerror: maximum recursion depth exceeded", when executed next code.

class property(object): def __init__(self): self.x = "raj" def gettx(self): print "getting x" homecoming self.x def settx(self, val): print "setting x" self.x = val def dellx(self): print "deleting" homecoming self.x x = property(gettx, settx, dellx, "i'm object property") p = property() print "p.x", p.x p.x = "r" print "p.x:", p.x

is not possible apply property in way. because worked fine when 'self.x' changed self._x , self.__x.

the error due next infinite recursion loop: have defined property x uses gettx, settx , deltx access methods, access methods seek access property x (i.e. phone call themselves).

you should write code along next lines:

class property(object): def __init__(self): self.__x = "raj" # class private def gettx(self): print "getting x" homecoming self.__x def settx(self, val): print "setting x" self.__x = val def dellx(self): print "deleting" homecoming self.__x x = property(gettx, settx, dellx, "i'm object property")

python

No comments:

Post a Comment