gpt4 book ai didi

python - Python 中的全局静态变量

转载 作者:太空狗 更新时间:2023-10-30 00:24:39 25 4
gpt4 key购买 nike

def Input():
c = raw_input ('Enter data1,data2: ')
data = c.split(',')
return data

我需要在其他函数中使用list data,但又不想每次都输入raw_input。我如何使 data 像 c++ 中的 global static 一样,并将其放在需要的地方?

最佳答案

将全局关键字添加到您的函数中:

def Input():
global data
c = raw_input ('Enter data1,data2: ')
data = c.split(',')
return data

global data 语句是使data 成为全局变量的声明。调用 Input() 后,您将能够在其他函数中引用 data

关于python - Python 中的全局静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3694580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com