を「最大の再帰の深さを超えて、」私はPythonスクリプトで@properties
とsetter
機能に精通になったために書いたテストクラスである:問題ここ
class Test(object):
def __init__(self, value):
self.x = value
@property
def x(self):
return self.x
@x.setter
def x(self, value):
self.x = value
は私がしたいときということです私のクラスからオブジェクトを作成し、私は次のエラーに直面:
>>> t = Test(1)
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
t = Test(1)
File "<pyshell#18>", line 3, in __init__
self.x = value
File "<pyshell#18>", line 9, in x
self.x = value
File "<pyshell#18>", line 9, in x
#A bunch of lines skipped
RuntimeError: maximum recursion depth exceeded
>>>
privateメンバーに 'self.x'の代わりに' self._x'を使用してください。メンバとプロパティの 'x'プロパティシャドーメンバの両方に名前を付けることで、ゲッター本体の' return self.x'はそれ自身を呼び出します。 –