2.3.10 Python 函数进阶-名称空间&作用域

名称空间

又名 name space。

说明:若变量 x=1, 1存放于内存中,那名字x存放在哪里? 

          名称空间正是存放名字x与1绑定关系的地方

 

名称空间共三种:

  • locals: 是函数内的名称空间,包括局部变量和形参
  • globals: 全局变量,函数定义所在的模块的名字空间
  • builtins: 内置模块的名字空间

不同变量的作用域不同,就是由于这个变量所在的命名空间决定的

作用域的范围

  • 全局范围:全局存活,全局有效
  • 局部范围:临时存活,局部有效

查看作用域的方法 globals(),locals(),dir(__builtins__)


 

作用域的查找顺序

 1 level = 'L0'
 2 n = 22
 3 
 4 
 5 def func():
 6     level = 'L1'
 7     n = 33
 8     print(locals())
 9 
10     def outer():
11         n = 44
12         level = 'L2'
13         print(locals(),n)
14 
15         def inner():
16             level = 'L3'
17             print(locals(),n) #此外打印的n是多少?
18         inner()
19     outer()
20 
21 
22 func()

问题:在inner()里的打印的n的值是多少?

LEGB 代表名字查找顺序: locals -> enclosing function -> globals -> __builtins__

  • locals 是函数内的名字空间,包括局部变量和形参
  • enclosing 外部嵌套函数的名字空间
  • globals 全局变量,函数定义所在模块的名字空间
  • builtins 内置模块的名字空间

 

转载于:https://www.cnblogs.com/devopsxin/p/9466439.html

(jqxx) D:\ProgramData\anaconda3\envs\jqxx>pip install https://github.com/pybox2d/pybox2d/releases/download/2.3.10/Box2D-2.3.10-cp39-cp39-win_amd64.whl Looking in indexes: https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple Collecting box2d==2.3.10 WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1147)'))': /pybox2d/pybox2d/releases/download/2.3.10/Box2D-2.3.10-cp39-cp39-win_amd64.whl WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1147)'))': /pybox2d/pybox2d/releases/download/2.3.10/Box2D-2.3.10-cp39-cp39-win_amd64.whl WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1147)'))': /pybox2d/pybox2d/releases/download/2.3.10/Box2D-2.3.10-cp39-cp39-win_amd64.whl WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1147)'))': /pybox2d/pybox2d/releases/download/2.3.10/Box2D-2.3.10-cp39-cp39-win_amd64.whl WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1147)'))': /pybox2d/pybox2d/releases/download/2.3.10/Box2D-2.3.10-cp39-cp39-win_amd64.whl ERROR: Could not install packages due to an OSError: HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: /pybox2d/pybox2d/releases/download/2.3.10/Box2D-2.3.10-cp39-cp39-win_amd64.whl (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1147)')))
06-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值