当同一个模块多次导入同一个环境时,模块和作用域如何交互? [英] How do modules and scope interact when the same module is imported into the same environment multiple times?
问题描述
假设我们有模块 A
、B
和 C
,并且该模块 C
有一些变量 X
在模块范围内.
Imagine we have modules A
, B
, and C
, and that module C
has some variable X
at module scope.
A
导入 B
,后者(出于自身目的)导入 C
.A
也直接导入C
.
A
imports B
, which (for its own purposes) imports C
. A
also directly imports C
.
如果A
改变了C.X
的值,那会改变B
看到的C.X
的值吗?同样,如果 B
改变了它的 C.X
值,那会改变 A
看到的值吗?
If A
changes the value of C.X
, does that change the value of C.X
that B
sees? Likewise, if B
changes its value of C.X
, does that change the value that A
sees?
换句话说,当一个模块多次导入同一个解释器上下文时,该模块命名空间中的可变变量是否有效地共享内存?
In other words, when a module is imported multiple times into the same interpreter context, do the mutable variables in that module's namespace effectively share memory?
推荐答案
如果
A
改变了CX
的值,那会改变B
看到的CX
的值吗?
If
A
changes the value ofC.X
, does that change the value ofC.X
thatB
sees?
tl;dr:
这取决于你如何导入 X
,以及X
的可变性.
如果A
和B
都用import C
语句导入C
模块,他们都可以访问并修改CX
.名称X
绑定到C
模块,A
和B
都可以访问该模块.示例:
If both A
and B
import the C
module with a import C
statement, they both can access and modify C.X
. The name X
is bound to the C
module, which both A
and B
have access to. Example:
C.py
X = 1
B.py
import C
def func():
print(C.X)
A.py
import B
import C
C.X = 2
B.func()
运行 A.py
打印 2
- A
有效修改了 C 的
模块的命名空间,此更改在调用 X
属性func()
时反映在 B
中.
Running A.py
prints 2
- A
effectively modified the X
attribute of the C
module's namespace, and this change is reflected in B
when calling func()
.
from _ import _
语句从外部模块获取属性并将其绑定到当前模块.当模块A
从C
导入X
时,它只是将该值绑定到A
的命名空间.换句话说,对 A
命名空间中的 X
的更改不会反映对 B.X
或 C.X
的更改.示例:
The from _ import _
statement takes an attribute from an external module and binds it to the current module. When module A
imports X
from C
, it simply binds that value to A
's namespace. In other words, changes to X
in A
's namespace will not reflect on changes to B.X
or C.X
. Example:
C.py
X = 1
B.py
from C import X
def func():
print(X)
A.py
import B
from C import X
X = 2
B.func()
运行 A.py
打印 1
,因为 B.func()
将打印存储在 BX
,我们只修改了AX
.
Running A.py
prints 1
, because B.func()
will print the value stored at B.X
, and we've only modified A.X
.
如果 X
是可变的(列表、字典、用户定义的类或实例等),那么你如何导入它并不重要 - 在任何模块中改变导入的对象都会传播跨越所有模块.示例:
If X
is mutable (lists, dictionaries, user-defined classes or instances, etc), then it doesn't really matter how you import it - mutating the imported object in any module will propagate across all modules. Example:
C.py
X = {} # mutable object (empty dictionary)
B.py
from C import X
def func():
print(X)
A.py
import B
import C
C.X['key'] = 'value'
B.func()
运行 A.py
打印 {'key': 'value'}
.我们有效地修改了同一个对象,即使 A
使用了 import _
语句,而 B
使用了 from _ import _
语句.对于任何可变对象都是如此,无论 A
或 B
如何导入它.
Running A.py
prints {'key': 'value'}
. We've effectively modified the same object, even though A
used a import _
statement, and B
used a from _ import _
statement. This is true for any mutable object, no matter how A
or B
imports it.
这篇关于当同一个模块多次导入同一个环境时,模块和作用域如何交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!