当前位置: 首页 > 图灵资讯 > 行业资讯> python中怎么定义很多全局变量

python中怎么定义很多全局变量

发布时间:2025-03-04 22:13:10

1.声明法

该方法是直接在当前模块中进行全球变量定义声明,然后引用Global声明!如下:

OLD_URL='http://oldboyedu.com'
defbb():
globalOLD_URL
OLD_URL=OLD_URL+'#m'
if__name__='__main__':
bb()
printOLD_URL
#输出:
http://oldboyedu.com#m

2.模块法

该方法是在一个单独的模块中定义全局变量,然后在需要使用的全局模块中导入定义的全局变量模块。如下:

#gl.py定义全局变量模块
GL_A=‘hello’
GL_B=’world’
#test.py全局变量引用模块
importgl
defhello_world()
printgl.GL_A,GL_B
输出“helloworld“

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09