主要.py:
import package
包/__init__.py:
# use function to split local and global namespace
def do_import():
print globals().keys()
print locals().keys()
import foo as mod
print locals().keys()
print globals().keys()
do_import()
包/foo.py:
print 'Hello from foo'
执行 main.py 会输出如下:
['__builtins__', '__file__', '__package__', '__path__', '__name__', 'do_import', '__doc__']
[]
Hello from foo
['mod']
['__builtins__', '__file__', '__package__', '__path__', '__name__', 'foo', 'do_import', '__doc__']
import
in__init__.py
没有按预期工作。请注意,全局命名空间有一个“foo”,它应该只绑定到本地“mod”
甚至
exec "import foo as mod" in {'__name__': __name__, '__path__': __path__}
无法阻止全局命名空间被修改
这怎么可能发生?