Python本地与全局命名空间用法实例
作者:小卒过河 时间:2022-01-05 10:01:12
本文实例讲述了Python本地与全局命名空间用法。分享给大家供大家参考。具体如下:
x = 1
def fun(a):
b=3
x=4
def sub(c):
d=b
global x
x = 7
print ("Nested Function\n=================")
print locals()
sub(5)
print ("\nFunction\n=================")
print locals()
print locals()["x"]
print globals()["x"]
print ("\nGlobals\n=================")
print globals()
fun(2)
///scope.py
Globals
=================
{'x': 1,
'__file__':
'C:\\books\\python\\CH1\\code\\scope.py',
'fun': <function fun at 0x008D7570>,
't': <class '__main__.t'>,
'time': <module 'time' (built-in)>,. . .}
Nested Function
=================
{'c': 5, 'b': 3, 'd': 3}
Function
=================
{'a': 2, 'x': 4, 'b': 3, 'sub':
<function sub at 0x008D75F0>}
4
7
希望本文所述对大家的Python程序设计有所帮助。
标签:Python,命名空间
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android应用开发中Action bar编写的入门教程
2022-01-03 02:25:18
python实现单线程多任务非阻塞TCP服务端
2021-05-29 07:26:52
跨浏览器实现float:center,No CSS hacks
2008-08-22 12:59:00
Python Opencv任意形状目标检测并绘制框图
2023-06-06 19:03:43
![](https://img.aspxhome.com/file/2023/7/92417_0s.jpg)
20个Javascript手风琴折叠菜单
2009-10-12 12:09:00
![](https://img.aspxhome.com/file/UploadPic/200910/12/javascript-22-33-23f-88s.jpg)
Python中利用Scipy包的SIFT方法进行图片识别的实例教程
2023-02-14 17:15:18
![](https://img.aspxhome.com/file/2023/5/72225_0s.jpg)
关于Python dict存中文字符dumps()的问题
2022-08-22 17:53:17
![](https://img.aspxhome.com/file/2023/7/79507_0s.png)
JS实现淘宝支付宝网站的控制台菜单效果
2023-07-22 22:26:12
![](https://img.aspxhome.com/file/2023/1/55941_0s.png)
python处理按钮消息的实例详解
2023-04-23 03:59:25
![](https://img.aspxhome.com/file/2023/9/86679_0s.png)
最新Python APScheduler 定时任务详解
2021-05-03 21:58:53
![](https://img.aspxhome.com/file/2023/3/69503_0s.png)
Python中用max()方法求最大值的介绍
2021-12-08 09:33:06
11个案例讲透Python函数参数小结
2023-08-01 13:38:10
Python3 ID3决策树判断申请贷款是否成功的实现代码
2021-02-11 15:01:58
![](https://img.aspxhome.com/file/2023/4/82914_0s.png)
scrapy与selenium结合爬取数据(爬取动态网站)的示例代码
2023-07-14 00:17:08
![](https://img.aspxhome.com/file/2023/7/79697_0s.png)
Python第三方库的几种安装方式(小结)
2021-11-01 19:34:09
SQL Server数据转换服务基本概念介绍
2009-01-20 15:52:00
tensorflow指定GPU与动态分配GPU memory设置
2023-01-16 11:30:24
python函数enumerate,operator和Counter使用技巧实例小结
2022-08-09 07:02:32
django 自定义filter 判断if var in list的例子
2022-09-25 16:11:37
MYSQL在一个字段值前面加字符串
2010-10-14 14:28:00