python higher order function

*Parameter transfer*

*Parameter transfer mechanism*

"""

The function parameter passing mechanism is the same as the assignment mechanism. Its essence is to pass the memory address, which is reference passing

When a passes in, it passes in the memory address

If you really don't understand, you can think it's x= a

"""

def isid(x):

#Memory address of print parameter

print(id(x))



a = 10

#Memory address of variable

print(id(a)) #140731588047408



#A becomes a parameter and prints the parameter memory address

isid(a) #140731588047408

*Default parameters*

#Define a function with default parameters

def add(x,y=10):

sum01 = x+y

return sum01



"""

1.add(2) x=2 y=10

2.add(2,3) x=2 y=3

"""

print(add(2)) #12

print(add(2,3)) #5

"""

Can you pass in a sequence type, such as list, and let it default to []

"""

def add(list01 = []):

list01.append(1)

return list01



#First call

print(add()) #[1]

#Second call

print(add()) #[1, 1]

#Third call

print(add()) #[1, 1, 1]

 

*Indefinite number of parameters*

*One**

"""

A *: represents a variable parameter (that is, it can be multiple values)

"""

def add(x,*y):

sum01 = x

for i in y:

sum01 += i

return sum01



print(add(1,2,3,4,5)) #15

*Two**

"""

**y means that any key value (Dictionary) can be used as a parameter

**y is equivalent to dictionary type

"""

def func_add(x,**y):

sum01 = x

#Remember that this is the cycle of the dictionary

for k,v in y.items():

print(k,v)

#Add v (value) to x

sum01 += v

return sum01

"""

call

a=3,b=4,c=5 ----> {"a":3,"b":4,"c":5}

"""

print(func_add(1,a=3,b=4,c=5)) #13

*name*

1. * when executing this file, name==main*

#Print the name attribute

#When this file is executed, it prints main, name == main

print("1,"+name) #1,main

*2. When import ed into another file, name = = file name*

"""

A file is a module. Importing a module is equivalent to importing a file, which can be written directly

import file name

"""

import this document as bwj

#Run the imported file name in other classes, name = = file name

print(bwj.name) # this document

*Higher order function*

*Definition*

A higher-order function is a function that takes a function as an argument or a function as a return value

If a function can be a parameter and a parameter is a variable, can a function be a variable

A parameter is actually a variable. Can a function be a variable

*Function as variable (supplementary)*

Function is a variable, not a high-order function, just to supplement this knowledge

"""

Function as variable

"""

#Define a function, pass in a parameter, and find the square of the parameter

def func_pf(x):

return pow(x,2)

#Dictionary

#Let's know that Li Si's age is the square of 5. Find Li Si's age

dict01 = {"Zhang San":20,"Li Si":func_pf}



"""

call

1.func_pf = dict01["Li Si"] according to key obtain value,vaule Is a function object

2.age02 = func_pf(5) #Call the function to get the age

"""

#The dictionary is characterized by obtaining value according to key

age01 = dict01["Zhang San"]

print(age01) #20



#As the name suggests, through Li Si, we can get the function func_pf

#What we get is a function object. We can't print it directly

func_pf = dict01["Li Si"]

print(func_pf) #<function func_pf at 0x000001E25A23D048>



age02 = func_pf(5)

print(age02) #25

*Function as parameter*

*Definition*

#Ordinary function

def func_pf(x):

return x**2

#Higher order function, with function as parameter

def func_add(func_pf):

#x**2+10

return func_pf+10



#2**2+10

a = func_add(func_pf(2))

print(a) #14

*Common higher order functions*

These commonly used higher-order functions are higher-order functions with functions as parameters

*m**ap(****)***

*Definition*

"""

map(fun,x) conversion, accept a sequence, convert it into a new sequence according to fun's functional logic, and return a map object

\1. x must be a sequence

\2. Return a map object

"""

*Code*

def func_add(x):

return x+1

"""

Put the original data through func_ After the add function, add 1 to each one, and then output a new list

"""

m = map(func_add,[1,2,3,4,5])

print(m) #<map object at 0x00000248E965C788>

print(list(m)) #[2, 3, 4, 5, 6]

*filter()*

*Definition*

"""

filter(func,x) filters, accepts a sequence x, and converts it according to the requirements of func function,

The func function returns True or False, and finally puts the element that returns True in the new list.

Return filter object

"""

*Code*

def func_ifScore(x):

#The comparison operator itself returns a Boolean value

#If it is greater than or equal to 60, it is true, otherwise it is False

return x>=60

f = filter(func_ifScore,[80,98,70,34,60,59,10])

print(f) #<filter object at 0x00000232CC78C808>

print(list(f)) #[80, 98, 70, 60]

*reduce()*

*Definition

reduce(func,x) calculation accepts a sequence and accumulates or performs other operations on each value in the list according to the function

Returns a calculated value

1. Because it is calculation, our func is two parameters

2. The package must be guided

*Code*

"""

#Cumulative 1 + 2 + 3 + 4 + 5

def func_add(x,y):

return x+y



a = reduce(func_add,[1,2,3,4,5])

print(a) #15



Tags: Python

Posted on Fri, 26 Nov 2021 12:58:11 -0500 by iandotcom