作用域

作用域

注意:函数,模块,类都有自己的作用域

for循环,while循环,if,else 都没有自己的作用域

def test1():
print("in the test1")
def test():
print("in the test")
return test1=====》test1 是函数test1的IP地址
s = test()========>运作test()结果是 in the test ,然后返回test1的IP,即目前在test1位置
print(s)=====》打印test1 的IP
所以结果是:

in the test
<function test1 at 0x7ff1e05d01e0>

******************************************************************

def test1():
print("in the test1")===>打印in the test1
def test():
print("in the test")====》打印in the test
return test1()=====>返回函数test1()
s = test()
print(s)===》函数test1()没有返回值,所以默认None
结果是:

in the test
in the test1
None

********************************************************************

def foo():
name = "lihaifeng" 第1步
def bar():
name = "wupeiqi"
print(name)
return bar 第2步,返回函数bar的位置
foo()======>运行函数foo(),首先进行第一步然后进行第二步,中间没用运行,所以不会输出结果
print(foo())=====》打印函数bar的位置,所以结果是函数bar的IP
foo()()====>foo()即bar 所以相当于调用函数bar(),结果是wupeiqi.

注意:函数外部不执行里边肯定不执行。里边执行,外边肯定执行

 for循环没有作用域,赋值按照最后一个值例如:

i = 0
for i in range(5):
i += 1
print(i)#---结果是5

 

posted @ 2021-03-07 10:14  wode110  阅读(64)  评论(0编辑  收藏  举报