我想覆蓋對類中一個變量的訪問,但正常返回所有其他變量。如何使用 __getattribute__
完成此操作?
我嘗試了以下操作(這也應該說明我正在嘗試做什麼)但我得到一個遞歸錯誤:
class D(object): def __init__(self): self.test=20 self.test2=21 def __getattribute__(self,name): if name=="test": return 0. else: return self.__dict__[name] >>> print D().test 0.0 >>> print D().test2 ... RuntimeError: 最大遞歸深度超出 cmp