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.