Python: maximum recursion depth exceeded while calling a Python object when calling copy function

后端 未结 1 1958
夕颜
夕颜 2020-12-11 10:23

I have a class Particle which has some parameters and attributes, as you can see below. But, when it does get to the function setter for position, and it executes t

相关标签:
1条回答
  • 2020-12-11 10:56

    It looks like you're trying to use position as the name of both the property and the ordinary attribute backing it. For example,

    @position.setter
    def position(self, newPosition):
        self.position = newPosition.copy()
    #   ^^^^^^^^^^^^^^^
    

    This attempt to set self.position will use the setter you're defining! Similarly,

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

    This getter just calls itself!

    Trying to use self.position inside the position property definition won't bypass the property. If you want a "regular" attribute backing the property, call it something else, like self._position or something.

    0 讨论(0)
提交回复
热议问题