import overwrites __name__

M

Marco

Hi,

There happened something that I do not understand. Actually I don't even
know how it can be possible.

I import a module and then the name space of the importing module seems do
be overwritten.

my_name = __name__
print my_name
print len(dir())
from x import y as z
print __name__
print len(dir())
print my_name

->
__main__
119
x
117
unhandled NameError "name 'my_name' is not defined"

The module x is from me, and I am not aware of doing anything cruel there.
What evil thing can be done in its sub module that can result in that
strange thing?

br
Marco
 
G

Glenn Hutchings

I import a module and then the name space of the importing module seems do
be overwritten.

my_name = __name__
print my_name
print len(dir())
from x import y as z
print __name__
print len(dir())
print my_name

->
__main__
119
x
117
unhandled NameError "name 'my_name' is not defined"

Interesting. Here's one possibility for the contents of module x.py:

import sys
del sys.modules['__main__'].my_name
y = 42

I can't think of any other way apart from grovelling through sys.modules.

Glenn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top