# # Higher Order function
-functions that accept functions as arguments are called higher-order functions
def fn (num): # receives parameters and prints print (num)def# The Accept function is a parameter # ) # Ten
-in Python, the function name is also a variable, the function body is the value of the variable, the variable can be re-assigned to replace the original binding
def fn (num): # defines a function print(num) fn (# execution function, output 10 = Ten # assigns the function name, the variable FN, to unbind FN from the function body fn (TEN) # FN does not point to the function body, Unable to invoke "" "" "" ""Traceback (most recent called last): ' File ' <stdin> ', line 1, in <module>typeerror : ' int ' object is not callable # error, type wrong: integer object cannot be called "" "
# # commonly built-in high-order functions
-Map (FN, iterable)
-This function accepts two parameters, the first argument is a function, the second argument is an iterative object
-When the map () function executes, it will iterate through the parameters of the object one at a time to pass in the first function parameter as a parameter and execute the parameter function
def FN (n): return n * n Map (FN, [1, 2, 3]) # computes the square of the list element # <map object at 0x7f5ee58e5b00>list (map (FN, [1, 2, 3]))# [1, 4, 9]list (map (STR, [1, 2, 3])) # converts all values in a list to a string # [' 1 ', ' 2 ', ' 3 ']
-Reduce (FN, iterable), need to import from Functools before use
-This function accepts two parameters, the first argument is a function, the second argument is an iterative object
-The elements of the second parameter are accumulated by one of the first argument functions as a single value, and the result is returned
from Import Reduce def Add (A, b): return A + b reduce (add, [1, 2, 3, 4]) # equivalent (((1 + 2) + 3) + 4)# ten
-Filter (FN, iterable)
-Filter
-The elements of the second parameter are checked individually according to the condition of the first parameter function, and if the argument function returns True, the element is preserved or the element is
def even (n): if n% 2 = = 0 :return Truereturn False filter (even, [ /c10>1, 2, 3, 4, 5, 6])# <filter object at 0x7f57c68cc550>list (filter (even, [1, 2, 3, 4, 5, 6])# [2, 4, 6]
-Sorted (): Sort
numbers = [ -50, -43, Max, 188,]sorted (Numbers, key=abs) # Sort by absolute size # [20 , 23°c, -43, -50, 188]sorted (numbers, key=abs,reverse=true) # reverse order # [188,-50,- (a)
-Functools.partial (): Partial functions (partial function)
-based on the original function, add common keyword parameters to generate a new function
Import Functools # creates a new function that converts a binary numeric string to a decimal integer int2 = functools.partial (int, base=2) int2 ("10000000 " )#
# # intrinsic function
-Python allows functions to be defined inside functions
def outer (name): def inner (): return " Hi, {0} " . Format (name) return inner () outer ("Stanley")# ' Hi, Stanley '
# # Closed Package
-An intrinsic function can be thought of as a closure, a function that can be dynamically generated by another function, and can alter and store the value of a variable created outside the function.
defTimes (num):defInner (a):returnNum * A#intrinsic functions can use the value of an external function returnInner#return intrinsic functionTimes10= Times (10)#a function that dynamically generates a parameter multiplied by 10type (TIMES10)#<class ' function ' >>>> TIMES10 (5)# -times5= Times (5)#a function that dynamically generates a parameter multiplied by 10TIMES5 (5)# -
# # anonymous function: Lambda () function
-Lambda functions are anonymous functions expressed in a single statement, which can be used instead of short functions
Syntax
Lambda parameter: return value
Lambda
Equivalent:
def double (x): return x * 2
-Previous high-order functions can also be passed directly using lambda as a parameter
List (map (lambda x:x * 2, [1, 2, 3, 4]))# [2, 4, 6, 8]
This article refers to:
Beauty Bill Lubanovic "Python language and its application"
Https://www.liaoxuefeng.com Liaoche's official website
Python-Functional programming