在下面的代码片段中,我在x属性上使用了一个setter(我想保持私有)
class test:
def __init__(self, pos, x):
self._pos = pos
self.x = x # Want to be a private variable, eg. self._x
@property
def x(self):
return self._x
@x.setter
def x(self, x):
# Logic for setting x
if self._pos == 'long':
self._x = -1 * abs(x)
elif self._pos == 'short':
self._x = abs(x)
else:
raise ValueError('$$ pos must be long or short')
问题是我最终得到两个属性self.x和self._x。由于我希望x是私有的-我只想拥有self._x(并放弃self.x)。代码中缺少什么?