Each module has its own global name space.
from module1 import some_function
x=10
some_function()
some_function
uses module1.x
in its definition, but you are setting x
in the current module. This would work:
from module1 import some_function
import module1
module1.x = 10
some_function()
Note that you can't use from module1 import x
, then set x = 10
, because that import
simply initializes a new name x
to have the same initial value as module1.x
; x = 10
then gives a new value to the new variable.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…