1 name = ["a1", "a2", "a3", "a4"] 2 3 def ask_way(): 4 if len(name) == 0: 5 return "ask over" 6 name_res = name.pop(0) 7 print(name_res) 8 if name_res == "a3": 9 return "asked the right way" 10 res = ask_way() 11 return res 12 13 res1 = ask_way() 14 print(res1)
1 name = "ABC" 2 def test1(): 3 name = "123" 4 def test2(): 5 name = "mnb" 6 def test3(): 7 print(name) 8 return test3 9 return test2 10 11 print(test1()) 12 print(test1()()) 13 print(test1()()()) 14 print("-"*30) 15 test2 = test1() 16 print(test2) 17 test3 = test2() 18 print(test3) 19 print(test3()) 20 #输出结果: 21 ‘‘‘ 22 <function test1.<locals>.test2 at 0x000001271233A8B0> 23 <function test1.<locals>.test2.<locals>.test3 at 0x000001271233A4C0> 24 mnb 25 None 26 ------------------------------ 27 <function test1.<locals>.test2 at 0x000001271233A8B0> 28 <function test1.<locals>.test2.<locals>.test3 at 0x000001271233A4C0> 29 mnb 30 None 31 ‘‘‘
1 fun1 = lambda x,y:x+y 2 print(fun1(1,2)) 3 ‘‘‘ 4 3 5 ‘‘‘
补充:
1 def foo(x): 2 print(x) 3 def bar(y): 4 print(y) 5 6 foo(bar("ABC")) 7 #输出结果: 8 ‘‘‘ 9 ABC 10 None 11 ‘‘‘
1 def bar(): 2 print(123) 3 4 def foo(): 5 print(456) 6 return bar 7 8 n = foo() 9 print(n) 10 n() 11 #输出结果: 12 ‘‘‘ 13 456 14 <function bar at 0x00000248B03000D0> 15 123 16 ‘‘‘
原文:https://www.cnblogs.com/ding-yuan/p/14881710.html