Private Variables and Class-local References
Imagine you have a class with a private member:
class Foo:
__attr= 5
Inside the class, this attribute can be referenced as __attr
:
class Foo:
__attr= 5
print(__attr) # prints 5
But not outside of the class:
print(Foo.__attr) # raises AttributeError
But it's different if you use eval
, exec
, or execfile
inside the class:
class Foo:
__attr= 5
print(__attr) # prints 5
exec 'print(__attr)' # raises NameError
This is explained by the paragraph you quoted. exec
does not consider Foo
to be the "current class", so the private attribute cannot be referenced (unless you reference it as Foo._Foo__attr
).