Python “private” name mangling and instance vs class attributes
问题 I was writing a decorator that needs to access private variables and found this discrepancy. Can anyone explain this? (Python 2.5) Naming mangling works as expected for attributes defined in the class: >>> class Tester(object): ... __foo = "hi" >>> t = Tester() >>> t._Tester__foo 'hi' Instance attributes do not work (and this is the way we are supposed to do it right?) >>> class Tester(object): ... def __init__(self): ... self.__foo = "hi" >>> t = Tester() >>> t._Tester__foo AttributeError: