如何在函数中创建或使用全局变量?
如何在其他函数中使用一个函数中定义的全局变量?
未能在适当的地方使用全局关键字通常会导致UnboundLocalError。在第一次使用后重新分配局部变量时,UnboundLocalError中解释了这方面的精确规则。一般来说,当寻求解释时,请将其他问题作为该问题的副本,当某人只需要知道全局关键字时,请关闭该问题。
如何在函数中创建或使用全局变量?
如何在其他函数中使用一个函数中定义的全局变量?
未能在适当的地方使用全局关键字通常会导致UnboundLocalError。在第一次使用后重新分配局部变量时,UnboundLocalError中解释了这方面的精确规则。一般来说,当寻求解释时,请将其他问题作为该问题的副本,当某人只需要知道全局关键字时,请关闭该问题。
当前回答
全局变量很好-除了多处理
与不同平台/环境上的多处理相关的全局变量因为一边是Windows/Mac OS,另一边是Linux,这很麻烦。
我将用一个简单的例子向你展示这一点,指出我前段时间遇到的一个问题。
如果你想了解为什么Windows/MacOs和Linux上的情况不同需要知道的是,启动新进程的默认机制。。。
Windows/MacOs是“种子”Linux是“fork”
它们在内存分配和初始化方面有所不同。。。(但我不想谈这个此处)。
让我们看看这个问题/例子。。。
import multiprocessing
counter = 0
def do(task_id):
global counter
counter +=1
print(f'task {task_id}: counter = {counter}')
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=4)
task_ids = list(range(4))
pool.map(do, task_ids)
窗户
如果你在Windows上运行这个(我想也是在MacOS上),你会得到以下输出。。。
task 0: counter = 1
task 1: counter = 2
task 2: counter = 3
task 3: counter = 4
Linux系统
如果您在Linux上运行此程序,则会得到以下结果。
task 0: counter = 1
task 1: counter = 1
task 2: counter = 1
task 3: counter = 1
其他回答
作为附加组件,使用一个文件来包含所有本地声明的全局变量,然后导入为:
文件initval.py:
Stocksin = 300
Prices = []
文件getstocks.py:
import initval as iv
def getmystocks():
iv.Stocksin = getstockcount()
def getmycharts():
for ic in range(iv.Stocksin):
引用要显示更改的类命名空间。
在本例中,runner使用文件config中的max。我希望我的测试在跑步者使用时更改max的值。
main/config.py
max = 15000
主/运行程序.py
from main import config
def check_threads():
return max < thread_count
测试/runner_test.py
from main import runner # <----- 1. add file
from main.runner import check_threads
class RunnerTest(unittest):
def test_threads(self):
runner.max = 0 # <----- 2. set global
check_threads()
尽管这已经得到了回答,但我还是再次给出了解决方案,因为我更喜欢单线这是如果您希望在函数中创建全局变量
def someFunc():
x=20
globals()['y']=50
someFunc() # invoking function so that variable Y is created globally
print(y) # output 50
print(x) #NameError: name 'x' is not defined as x was defined locally within function
您需要在每个要使用的函数中引用全局变量。
如下:
var = "test"
def printGlobalText():
global var #wWe are telling to explicitly use the global version
var = "global from printGlobalText fun."
print "var from printGlobalText: " + var
def printLocalText():
#We are NOT telling to explicitly use the global version, so we are creating a local variable
var = "local version from printLocalText fun"
print "var from printLocalText: " + var
printGlobalText()
printLocalText()
"""
Output Result:
var from printGlobalText: global from printGlobalText fun.
var from printLocalText: local version from printLocalText
[Finished in 0.1s]
"""
全局变量很好-除了多处理
与不同平台/环境上的多处理相关的全局变量因为一边是Windows/Mac OS,另一边是Linux,这很麻烦。
我将用一个简单的例子向你展示这一点,指出我前段时间遇到的一个问题。
如果你想了解为什么Windows/MacOs和Linux上的情况不同需要知道的是,启动新进程的默认机制。。。
Windows/MacOs是“种子”Linux是“fork”
它们在内存分配和初始化方面有所不同。。。(但我不想谈这个此处)。
让我们看看这个问题/例子。。。
import multiprocessing
counter = 0
def do(task_id):
global counter
counter +=1
print(f'task {task_id}: counter = {counter}')
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=4)
task_ids = list(range(4))
pool.map(do, task_ids)
窗户
如果你在Windows上运行这个(我想也是在MacOS上),你会得到以下输出。。。
task 0: counter = 1
task 1: counter = 2
task 2: counter = 3
task 3: counter = 4
Linux系统
如果您在Linux上运行此程序,则会得到以下结果。
task 0: counter = 1
task 1: counter = 1
task 2: counter = 1
task 3: counter = 1