python---函数作用域
2024-08-28 14:35:57
1、作用域
local:局部作用域
E(Enclosing):闭包函数外的函数中
G(global): 全局作用域
B(Build-in):內建作用域
查找变量的顺序,从上到下
2、函数内的变量只能在函数内部调用
3、
a = 'hello' def hi():
b = 'world'
print(b)
print(a) #先在函数内部找,找不到在全局变量中找
print(locals()) #函数内部的局部变量
print(globals()) #全局变量 hi() 控制台输出:
world
hello
{'b': 'world'}
{'a': 'hello', '__cached__': None, '__package__': None, '__spec__': None, 'hi': <function hi at 0x000000000348CBF8>, '__name__': '__main__', '__loader__': <_frozen_importlib.SourceFileLoader object at 0x00000000034E0550>, '__builtins__': <module 'builtins' (built-in)>, '__doc__': None, '__file__': 'D:/script/kecheng/lesson3/function.py'}
4、return:用于函数结尾,函数内return语句后面的代码不会被执行
def test():
return 'hahaha'
print('yayayay') test() 控制台输出:为空
最新文章
- Head First 设计模式之适配器模式与外观模式
- 约瑟夫问题(c++实现)
- Codeforces Round #117 (Div. 2)
- bzoj1057,poj3250
- linux命令 -->; pwd命令
- FFT小结
- sql Server 发送邮件 错误类型及原因
- htop安装步骤【原创】
- CoreData归纳使用
- NodeJs的async
- 基于Spring Cloud、JWT 的微服务权限系统设计
- .NET Core IdentityServer4实战 第三章-使用EntityFramework Core进行持久化配置
- 移动开发day4_京东移动页面
- Python:Day16 闭包、装饰器
- angular-cli 正确安装步骤
- WebForm、MVC、流式计算
- 转:PHP中的使用curl发送请求(GET请求和POST请求)
- Notebook computer(Ubuntu)
- Kali更新与升级
- 【BZOJ 3229】 3229: [Sdoi2008]石子合并 (GarsiaWachs算法)