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: 'Tester' object has no attribute '_Tester__foo' P.S. Is "class attribute" the right word for these? They aren't static, but if you make one of those a list, or some other mutable type, it is shared...
Update
In fact, second example works fine, too. It was a hardware issue (restart helped).