Edit: Both current answers talk about the fact that there is mangling inside classes. My question is about values inside class methods - note that the parsing in the method scope is different from the class scope, or __CLASS would be printable.
This seems to be the case in python 2.7 and 3.6.
For example, this code
__GLOBAL = 'global'
_Bar__MANGLED_GLOBAL = 'mangled global'
class Bar(object):
__CLASS = 'class'
def baz(self):
__LOCAL = 'local'
try:
print __LOCAL
except Exception as e:
print e
try:
print __CLASS
except Exception as e:
print e
try:
print __GLOBAL
except Exception as e:
print e
try:
print __MANGLED_GLOBAL
except Exception as e:
print e
Bar().baz()
Will give
local
global name '_Bar__CLASS' is not defined
global name '_Bar__GLOBAL' is not defined
mangled global
I understand the logic when there is an other.__x, or needing a class-level __foo to equate to self.__foo, but this seems like an oversight, where anything inside the class scope is automatically rewritten.
This was not an oversight. In fact, private globals used to be specifically advertised in the tutorial:
There is now limited support for class-private identifiers. Any identifier of the form
__spam(at least two leading underscores, at most one trailing underscore) is now textually replaced with_classname__spam, whereclassnameis the current class name with leading underscore(s) stripped. This mangling is done without regard of the syntactic position of the identifier, so it can be used to define class-private instance and class variables, methods, as well as globals, and even to store instance variables private to this class on instances of other classes.
Without specific references, like a mailing list conversation or something, we can only speculate as to why it was designed this way. (I checked the commit history, but the original commit includes no justification for design choices.) We can speculate that it was to enable things like private globals, but we can also speculate that it was just easiest to implement it this way and that the implementers wouldn't have been swayed by the prospect of private globals if other options were easier. It would certainly have been a lot harder to implement name mangling in a way that only affected the class's instance and class attributes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With