Python 导入 : Will changing a variable in "child" change variable in "parent"/other children?

标签 python import

假设您有 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/

相关文章:

python - flask 正确配置日志记录

python - 防止在 GAE/memcache 上丢失更新/竞争条件的策略/模式

python - 正确使用 scipy.interpolate.RegularGridInterpolator

python - Python 如何处理来自 "elsewhere"的对象

python - pandas 数据框样式 - 如何单独对每一行进行颜色映射(不是整体在一起)

python - 如何用汉字打印 tesseract 结果

eclipse - 导入 Guava 源时无法解析导入 javax.inject

python - 你能导入一个 .so 文件吗?

python - 本地与全局进口 python

xcode - 在 iOS Cocoa 静态库项目中使用 Objective-C 代码,在 iOS 应用程序项目中使用 Swift 代码