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':
4、return:用于函数结尾,函数内return语句后面的代码不会被执行
def test():
return 'hahaha'
print('yayayay')
test()
控制台输出:为空
手机扫一扫
移动阅读更方便
你可能感兴趣的文章