Python导入:将更改“child”中的变量改变“父母”/其他孩子的变量? [英] Python imports: Will changing a variable in "child" change variable in "parent"/other children?

查看:115
本文介绍了Python导入:将更改“child”中的变量改变“父母”/其他孩子的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有3个模块,a.py,b.py和c.py:

Suppose you have 3 modules, a.py, b.py, and 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中的v1和b.py?如果没有,有办法吗?

Will c.py change v1 in a.py and b.py? If not, is there a way to do it?

推荐答案

所有声明如下:

v1 = 0

可以做将名称 v1 绑定到对象 0 。它不会影响不同的模块。

can do is bind the name v1 to the object 0. It can't affect a different module.

如果我在那里使用不熟悉的术语,我想我可能是,我强烈建议你阅读Fredrik Lundh的优秀文章< a href =http://effbot.org/zone/python-objects.htm =nofollow noreferrer> Python Objects:重置你的大脑。

If I'm using unfamiliar terms there, and I guess I probably am, I strongly recommend you read Fredrik Lundh's excellent article Python Objects: Reset your brain.

这篇关于Python导入:将更改“child”中的变量改变“父母”/其他孩子的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆