How to forbid the assignment to some attributes and update linked attributes of a Python object?
本问题已经有最佳答案,请猛点这里访问。
例如,
1 | c = myClass() |
号
1 2 3 4 5 | c.a = 10 # or setattr(c,'a',10) or c.__setattr('a',10) # c.b becomes 5 c.b = 10 # c.a becomes 20 |
号
你要找的是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class MyClass: def __init__(self, x, a): self._x = x self.a = a @property def x(self): return self._x @property def b(self): return self.a / 2 @b.setter def b(self, b): self.a = b * 2 |
没有用于