作用域

时间:2021-03-08 13:36:19   收藏:0   阅读:0

作用域

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.

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

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!