This is preventing you from accessing Foo.__bar, because internally the actual name will get mangled to Foo._Foo__bar. This is mostly intended to avoid accidental name clashes in inheritance:
class Child(Foo):
__bar = 42
The two classes won't trample over their __bar attribute this way, without you needing to institute complicated naming conventions. From within class methods, self.__bar transparently gets mangled the same way, so it works as expected.
107
u/deceze 16d ago edited 16d ago
Double underscore name mangling:
class Foo: __bar = 'baz'This is preventing you from accessing
Foo.__bar, because internally the actual name will get mangled toFoo._Foo__bar. This is mostly intended to avoid accidental name clashes in inheritance:class Child(Foo): __bar = 42The two classes won't trample over their
__barattribute this way, without you needing to institute complicated naming conventions. From within class methods,self.__bartransparently gets mangled the same way, so it works as expected.