如何导入辅助模块以使用需要从第一个执行模块更新全局变量的函数

How to import a secondary module to use a function which requires updated global variables from the first executing module

我有一个导入模块 B 的模块 A。模块 A 具有不断变化的全局变量。我需要从模块 B 中 运行 的函数需要这些变量的更新值。我正在尝试这样的事情:

模块 A.py:

test_var = 0

def updateA():
    import B
    B.update()

if __name__ == "__main__":
    for _ in range(100):
        updateA()
        print(test_var)

模块B.py:

import A
def update():
    A.test_var += 1

应该打印从 0 到 00 的数字。 这只是我要完成的简化示例。

而不是直接引用模块 A 中的变量。将变量作为参数传递给更新函数。

例如模块 A.py:

test_var = 0

def updateA():
    global test_var // Ensure usage of global scope
    test_var = test_var + 1
    import B
    B.update(test_var)

if __name__ == "__main__":
    for _ in range(100):
        updateA()
    print(test_var)

并在模块 B.py

def update(var)
    print(var)