day11 作用域

返回值可以任何类型,返回可以是函数,返回函数还可以再被调用
仅仅返回函数是无法运行的。要运行需要加上()
没有返回值的时候,默认返回值为空,None
 1 def test1():
 2     print("in the test1")
 3 def test():
 4     print("in the test")
 5     return test1
 6 print(test)      # <function test at 0x0000000002652268> 返回一个对象
 7 res = test()     # 执行对象,执行函数得到返回值 test1这个对象,不信你看下面的打印
 8 print(res)       # <function test1 at 0x000000000200C1E0> 和test1特么一样的
 9 print(test1)     # <function test1 at 0x000000000205C1E0> 没骗你吧
10 print(res())     # res()相当于再执行 test1()
 1 name = "alex"
 2 def foo():
 3     name= "yangtuo"
 4     def     bar():
 5         # name = "tuo"
 6         print(name)
 7     return bar    # 返回一个这个bar的内存地址
 8 a = foo()         # 拿到内存地址
 9 print(a)          # 打印出来看一眼 <function foo.<locals>.bar at 0x00000000025C96A8>
10 a()              # 运行内存地址的代码
 1 def foo():
 2     name = "lhf"
 3     def bar():
 4         name = "wupeiqi"
 5         def tt():
 6             print(name)
 7         return  tt
 8     return bar
 9 
10 bar = foo()
11 tt = bar()
12 print(tt)
13 tt()
14 
15 foo()()() # 等同于上面的4命令

原文地址:https://www.cnblogs.com/shijieli/p/9687773.html