
正文
python---函数作用域
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
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() 控制台输出:为空





