我想将引用导入一个对象,其值在导入引用的初始文件被python解析后更新。例如:
runner.py
from store import modify_var
from importer import a_function
print('>>runner')
modify_var()
a_function()
store.py
my_var = None
def modify_var():
global my_var
my_obj = MyObj()
my_var = my_obj
print('>>store, my_var has been set to: {0}'.format(my_var))
class MyObj(object):
x = 2
进口商。派克
from store import my_var
print('>>importer, my_var outside function (before has been set): {0}'.format(my_var))
def a_function():
print('>>importer, my_var after is has been set: {0}'.format(my_var))
运行runner后的控制台输出。派克
>>importer, my_var outside function (before has been set): None
>>runner
>>store, my_var has been set to: <tests.store.MyObj object at 0x7fbfc683a1d0>
>>importer, my_var after is has been set: None
我试图弄清楚如何导入我的_var,这样当我在函数中调用它时,它的值就不是None了。
我认为它不喜欢在存储中给我的变量赋值后声明它。皮耶。请尝试以下操作之一:
在store.py中,创建另一个返回my_var为no的方法:
def my_var_none():
my_var = None
return my_var
然后,离开跑步者。现在是py,内部是进口商。py:
from store.py import my_var_none()