Wudao road-five-day peak during the refining stage
A function is a variable.
Function nesting: Define a function call in a function. (multiple layers can be nested. Note that local scopes and global scopes are distinguished)
Def test ():
Def testx ():
Test ()
High-order functions: pass a function name as a real parameter to another function or the function return value contains the function name.
Def f_test (func ):
Func ()
Def f_test1 ():
Print ('aaa ')
F_test (f_test1) # z calls f_test here, And f_test1 will be called in function f_test (without changing its source code and outputting aaaa)
Def f_test2 (func ):
Print ('some functions ')
Return func
F_test1_address = f_test2 (f_test1) # print f_test1_address. We can see that when f_test2 is called, The passed real parameters will be returned.
F_test1_address () # equals to execute f_test2, f_test1 (do not change its call method and add some output functions)
Decorator (Higher-order functions + nested Functions): Do not change the function call method and source code;
Def f1 (func): # decorator
Def f2 ():
Func ()
Print ('function ')
Return f2
@ F1 # This is equivalentF3 = f1 (f3 ))That is, add the name of the @ modifier before the function definition to Add decoration to the function.
Def f3 ():
Print ('f3 ')
F2_address = f1 (f3) =>Define An f2 in the f1 function and return f2 (and f2 runs f3 (the passed function name and the passed real parameter is f3 ), and outputs functions)
At this time, f2_address () is equivalent to running f2, that is, running and outputting the function. If you replace f2_address with f3 (F3 = f1 (f3 ))
Then calling f3 will execute one more print ('function') (here you can write your additional features)