I am playing around with python's namespacing and I have come across some behaviour I can't explain. In particular I wanted to see if you can stop classes being imported with a command like 'from module import *'. In one module, call it moduleA, I have the following:
class __hi(object): pass
class hey(object):
def __init__(self):
# self.hey = __hi()
Then in another module, say moduleB, I import 'everything' from moduleA with
from moduleA import *
This has the desired effect of importing the class 'hey' but not the class __hi.
Now when the line above '# self.hey = __hi()' is uncommented I get an error I don't understand:
"NameError: global name '_hey__hi' is not defined"
It looks as though python has mangled the variable name because the class name has a double underscore. Can anyone explain this?
This question is completely different from that which is referenced. In the linked post presented the name is mangled with the class variable in which the mangled variable is living. That is not what I am asking about.
Edit:
Thanks to vaultah for pointing out that: It doesnt matter where the double-underscore is in the line it will still trigger name-mangling - but could anyone explain why this is the case? It means that, in (highly contrived) situations like the one above, you can never save an instance of a class in another class.