If I define a Python class which has an instance field/variable __MAX_N
, it seems Python applies this private name mangling thing. But if I rename it to __MAX_N__
then there's no mangling, and I can access it freely from the outside?! That kind of surprised me so I wonder if __MAX_N__
is supposed to designate (by Python convention) something else say a constant or something else?
So I mean...
- Why is
__A__
not mangled and__A
is?
Also...
- Is there mangling for class (non-instance) fields?
- Is there mangling for method names?
I mean specifically in Python 3.x, not interested in 2.x.
EDIT:
I was pointed to this part of the Python docs.
Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped
So why are the __A__
instance fields not mangled? What is the idea behind them?