Function object:
1 , can be assigned
f=func print(f,func) f()
2 , can be used as the function as an argument to another function
def foo(x): # print(x) x()
3 , can be used as additional function as a return value of the function
def foo(x): return x res=foo(func) print(res) res()
4 , elements of the container can be used as a type of
l = [function,] # print(l) l[0]() name = { ' k1 ' : func} print (say) I say [ ' k1 ' ] ()
Nested functions:
1 , nested function calls: calling a function of the process and call other functions
def max2(x,y): if x > y: return x else: return y def max4(a,b,c,d): res1=max2(a,b) res2=max2(res1,c) res3=max2(res2,d) return res3 res = max4 (1,2,3,4 ) print(res)
2 , the nested function definition : to define other functions within a function
def f1(): def f2(): pass
Closure function:
Closure function = namespace and scope + recursive function + function object
The core point: the name of the relationship is to find the function definition stage prevail
" Closed " function refers to the function is built-in functions
" Pack " function refers to the function of the outer layer contains a reference to the name of the function scope
Closure function: namespace application and scope + nested functions
def f1(): x = 333 def f2(): print(x) f2() x=11111 def bar(): x=44 f1 () def foo(): x=2222 bar() foo()
Closure function: function objects
def f1(): x = 333 def f2(): print(x) return f2 f=f1() def foo(): x=5555 f() foo()
Two kinds of transmission parameters as a function of the way the body
Way: the parameter defines the function body directly molded required parameters
def f2(x): print(x) f2(1) f2(2) f2(3)
Second way: nested call
def f1(x): x=3 def f2(): print(x) return f2 x=f1(3) print(x) x()