假设您有 3 个模块,a.py、b.py 和 c.py:
a.py:
v1 = 1
v2 = 2
etc.
b.py:
from a import *
c.py:
from a import *
v1 = 0
c.py会不会改变a.py和b.py中的v1?如果没有,有什么办法吗?
最佳答案
像这样的声明:
v1 = 0
可以做的是将名称v1
绑定(bind)到对象0
。它不会影响其他模块。
如果我在那里使用不熟悉的术语,我想我可能是,我强烈建议您阅读 Fredrik Lundh 的优秀文章 Python Objects: Reset your brain .
关于Python 导入 : Will changing a variable in "child" change variable in "parent"/other children?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1251611/