Python全局变量关键字global的简单使用
作者:玩转测试开发 时间:2022-01-29 11:03:49
简介:
1、global是Python中的全局变量关键字。
2、全局变量是编程术语中的一种,源自于变量之分。
3、变量分为局部与全局,局部变量又可称之为内部变量。
4、由某对象或某个函数所创建的变量通常都是局部变量,只能被内部引用,而无法被其它对象或函数引用。
5、全局变量既可以是某对象函数创建,也可以是在本程序任何地方创建。全局变量是可以被本程序所有对象或函数引用。
6、global关键字的作用是可以使得一个局部变量为全局变量。
案例1:全局无法使用局部变量。
# -*- coding: utf-8 -*-
def test1():
# 局部变量 local
local_var = "a"
print(local_var)
# 全局无法使用局部变量,只有对应的局部作用域可用
# print(local_var) # NameError: name 'local_var' is not defined
案例2:全局变量,任意范围均可使用。
global_var = 1
def test2():
# 函数内使用全局变量
print(global_var + 1)
def inner():
# 嵌套函数内使用全局变量
print(global_var + 2)
return global_var + 3 # 返回值内使用全局变量
# 函数外使用全局变量。
print(global_var)
案例3:函数内定义的局部变量
def test3():
# 函数内变量,但对于下级函数就是全局变量,对于外部来说就是局部变量
func_var = 1
def inner():
print(func_var)
return func_var
return inner()
test3()
案例4:函数间global关键字的作用
def test4():
# global关键字作用
global func_var
func_var = 2
# 调用test5可以打印 func_var,去掉global会报错。
test5()
print(test5.__globals__)
def test5():
print(func_var)
test4()
案例5:不同文件模块中的global,注意test6, test7为不同文件。
# a.py
def test6():
# global关键字作用
global func_var
func_var = 3
# b.py
from a import test6
def test7():
print(test6.__globals__["func_var"])
# 不先执行test6的情况下会抛出异常。KeyError: 'func_var'
test7() # KeyError: 'func_var'
案例6:不同文件模块中的global,注意test6, test7为不同文件。
# a.py
def test6():
# global关键字作用
global func_var
func_var = 4
# b.py
from a import test6
def test7():
print(test6.__globals__["func_var"])
# 先执行test6的情况下,test可以使用 func_var
test6()
test7() # 4
附:用global声明多个变量需要用逗号分
num = 0
def cc():
global count,num
count = count+1
num = num+2
print(count,num)
cc()
3 2
# 可以函数中的global声明能够修改全局变量
num
Out[24]: 2
#
count
Out[25]: 3
在使用全局变量的场合,也可用类变量代替
class C:
count = 3
def cc():
count = C.count+1
print(count)
cc()
4
结论:
1、只导入包,global定义的全局变量没有被加到globals里面。
2、执行global所在的对应函数,global定义的函数内会存入对应变量,其他函数内则不会存入。
来源:https://blog.csdn.net/hzblucky1314/article/details/122724678
标签:python,global,全局变量关键字
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使链接具有最大化、最小化的功能代码
2008-02-24 16:43:00
Python使用内置json模块解析json格式数据的方法
2023-07-30 14:10:45
![](https://img.aspxhome.com/file/2023/6/61226_0s.png)
PHP中单引号和双引号的区别详解
2023-05-25 08:16:51
Go语言学习之反射的用法详解
2023-06-16 06:40:40
![](https://img.aspxhome.com/file/2023/1/86281_0s.png)
asp伪继承初探_实例代码
2011-04-19 10:32:00
使用Python3内置文档高效学习以及官方中文文档
2022-06-13 08:14:45
PHP getDocNamespaces()函数讲解
2023-06-13 22:19:06
![](https://img.aspxhome.com/file/2023/5/55405_0s.png)
请正确认识MySQL对服务器端光标的限制
2008-12-17 14:58:00
如何提升JavaScript的运行速度(递归篇)
2010-05-17 13:30:00
使用Python+selenium实现第一个自动化测试脚本
2021-01-26 17:52:01
![](https://img.aspxhome.com/file/2023/2/75902_0s.jpg)
Python使用pandas和xlsxwriter读写xlsx文件的方法示例
2022-05-14 00:35:25
![](https://img.aspxhome.com/file/2023/7/70227_0s.png)
Python import与from import使用和区别解读
2021-06-22 08:51:02
sql数据库批量处理脚本
2011-11-03 17:23:19
菜鸟课堂:MySQL权限的详细解答
2009-09-03 11:43:00
python闭包和装饰器你了解吗
2022-01-11 00:46:02
![](https://img.aspxhome.com/file/2023/2/71392_0s.jpg)
交互设计实用指南系列(3)—“有效性”之“适时帮助”
2009-12-25 14:29:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/04d4365a20d52fbefe705991235bec36-87s.gif)
如何使用PHP计算上一个月的今天
2023-11-15 14:16:25
10款实用的jQuery图片插件
2011-01-25 12:28:00
![](https://img.aspxhome.com/file/UploadPic/20111/25/jquery_image_2-33s.jpg)
详解Python中for循环的使用
2023-06-13 13:55:40
Pycharm搭建一个Django项目的方法步骤
2022-04-12 12:46:41
![](https://img.aspxhome.com/file/2023/1/81491_0s.jpg)