# module1.py
question = "What is the meaning of life, the Universe, and everything?"
answer = 42
# module2.py
question = "What is your quest?"
answer = "To seek the holy grail."
>>> import module1
>>> import module2
>>> print module1.question
What is the meaning of life, the Universe, and everything?
>>> print module2.question
What is your quest?
>>> print module1.answer
42
>>> print module2.answer
To seek the holy grail.
如果我们使用from module1 import*和from module2 import*,则会出现命名冲突,无法访问来自module1的问题和答案。为什么会这样?你知道吗
这就是为什么。你知道吗
一个基于@alexce示例的简单示例:
当前在全局命名空间中定义的变量:
现在我们进行基于
*
的导入:来自
math
的新pi
替换旧的pi
现在,全局名称空间充满了数学模块的变量:
因此,这样的导入将替换已在全局命名空间中定义的具有相同名称的所有变量。你知道吗
从docs:
相关问题 更多 >
编程相关推荐