Higher order function
Functions are first-class citizens in Python
Functions are also objects, callable objects
Functions can be used as ordinary variables, parameters, return values, etc.
Necessary conditions for becoming a higher-order function:
Receive one or more functions as parameters
Output a function
Examples:
def counter (base): def inc (step = 1 ): nonlocal base base + = step # base = base + step, use local variables, but there is no base local variables, you need to use nonlocal base, declared as not local variables outward find, is formed closure return Base return inc is F = counter (10 ) Print (F ()) # output . 11 F1 = inc is (. 5 ) F2 = inc is (. 5 ) F1 iS F2, F1 == F2, ID (F1) , the above mentioned id (F2) # function contents can not be compared, the comparison will be converted into a memory address that is converted into f1 is f2 result is False '' ' Both f1 and f2 point to the internal function inc. Every time the counter function is called, it needs to create a stack pin, push the stack, and return the function value to the top of the stack. After that, the counter function dies, but the closure f1 records the memory address of the inc function , Each time is a brand new function call,
so f1 f2 memory address is different, the content can not be compared between functions, it will be implicitly converted to compare memory address that is f1 == f2-> f1 is f2 '' '