开发者

Why can you reference an imported module using the importing module in python

I am trying to understand why any import can be referenced using the importing module, e.g

#module master.py
import slave

开发者_开发问答and then

>>>import master
>>>print master.slave

gives <module 'slave' from 'C:\Documents and Settings....'>

What is the purpose of the feature? I can see how it can be helpful in a package's __init__.py file, but nothing else. Is it a side effect of the fact that every import is added to the module's namespace and that the module's namespace is visible from the outside? If so, why didn't they make an exception with data imported from other modules (e.g don't show it as part of the module's namespace for other modules)?


It's a side effect, but it can be used purposefully, e.g. os.py imports either posixpath or ntpath as path in order to create os.path.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜