我遇到了在模块内声明的全局变量的问题,然后我需要a)在主脚本中更新并且b)在另一个模块中用作此更新的基础。我还没有找到任何其他主题有相同的问题/模块结构,所以我希望有人能够帮助我。
澄清一下,代码的简化版本:
main.py
import initialAB
import updateAB
import util
def fun1(iterate, argsList):
initialAB.init(a, b) # Initializes A and B
print(A, B)
# PROBLEM:
A, B, x = updateAB.update()
# I can't update A and B without first declaring them global variables
# but if I do so, print(A, B) yields: "name 'A' is not defined"
while iterate:
x_old = x
A, B, x = updateAB.update()
if x_old == x:
arg1 = False
util.write_to_file() # write final version A and B to file
print(fun2(argslist[0], argslist[1]))
fun1(True, someList)
initialAB.py
A = dict()
B = dict()
def get_dict(arg2, arg3):
if arg3 == A:
A = # something to initialize A
elif arg3 == B:
B = # something to initialize B
def init(a, b):
global A, B
get_dict(a, "A")
get_dict(b, "B")
updateAB.py
import initialAB
from initialAB import A, B
def fun2(someArg, otherArg):
# c and d calculated based on someArg, otherArg, but also A and B
return c, d
def update():
c, d = fun2(someArg, otherArg)
# use c and d to calculate some new version of A and B
newA = # something something
newB = # etc.
return newA, newB, newX
以前,我刚刚将更新的A和B写入updateAB.update()内的文件。但是为了更新main.py中的A和B然后在后续迭代中使用和更新它们,我需要将它们声明为全局变量。但是,如果我这样做,我将无法再访问由initialAB.init(a,b)初始化的A和B.
这可能是通过简单地从initialAB.init()返回A和B来阻止的,这就是我要做的。不幸的是,我没有写大部分内容,A和B的使用方式太多,我不想改变其他功能,所以我真的希望有人有另一个解决方案来解决这个问题。 谢谢!
答案 0 :(得分:0)
如果您有一个声明变量的脚本,则可以在导入脚本的任何其他脚本中将该变量简单地用作myscript.myvar
。通常这是在一个单独的脚本中完成的,除了声明变量之外,它不会执行任何操作。你可以有功能,例如使用你的initialAB
,但通常只有一个脚本用于变量,因为你必须在其他任何地方导入,并且可能并不总是需要这些功能。
我不会讨论所有事情,只是为了展示它的一般情况:
A = dict()
B = dict()
# Declare any variables you want share across scripts.
# Even placeholders will do since you can just overwrite their values:
x = 0
y = None
import params
import initialAB
import updateAB
import util
def fun1(iterate, argsList):
params.A, params.B = initialAB.init(a, b) # Initializes A and B
# Note: I changed it so that init has to return A and B.
# You could also change init to use params.var (for example)
print(params.A, params.B)
# PROBLEM:
params.A, params.B, x = updateAB.update()
# I can't update A and B without first declaring them global variables
# but if I do so, print(A, B) yields: "name 'A' is not defined"
while iterate:
x_old = x
params.A, params.B, x = updateAB.update()
if x_old == x:
arg1 = False
util.write_to_file() # write final version A and B to file
print(fun2(argslist[0], argslist[1]))
fun1(True, someList)
基本上,您只需将var
的变量引用重新编写为params.var
即可。
如果您不熟悉,另一种方法是使用课程。基本上你可以把你的主要脚本变成一个类,它带有你共享变量的定义。然后,其他脚本可以将这些变量用作myclass.myvar
,这在语法方面几乎相同。它还可以避免使用额外的params
脚本。
# imports
class mainclass():
def __init__(self):
# This code is run when you first call your class
self.A = dict() # Some variable declarations
self.B = dict()
self.fun1(True, someList)
# All function calls inside the class refer to self.functionname
# For variables this depends: If you use a variable only inside
# a function, you don't need it. If you do (like A and B), you must.
# self.var is an attribute of a class that persists even after the
# function that declared it exits. The other variables are cleared away.
def fun1(self, iterate, argsList): # First argument is always self
self.A, self.B = initialAB.init(a, b) # Initializes A and B
print(self.A, self.B)
self.A, self.B, self.x = updateAB.update()
while iterate:
x_old = self.x
self.A, self.B, self.x = updateAB.update()
if x_old == self.x:
arg1 = False
util.write_to_file() # write final version A and B to file
print(fun2(argslist[0], argslist[1]))
main = mainclass()
# Other scripts can refer to variables in mainclass as main.variable
# e.g.: main.A, main.B