According to the Teacher Liao Xuefeng’s Python tutorial for learning \
directory
- Higher-order functions
- map
- reduce
- filter
- sorted
- Lambda expressions
- A decorator
- The __name__ attribute of the function object
- Define decorator
- The most common decorator
- Decorator with text added
- Use decorators
- A way to keep the decorator definition of the __name__ attribute unchanged
- Decorator with no parameters
- Decorator with parameters
- Partial function
Higher-order functions
map
Enter an iterable(list,tuple, etc.) and a function
The corresponding function is executed on each element of the iterable
Returns the result and places it in the new iterator
def f(x) :
return x * x
Copy the code
r = map(f, [1.2.3.4.5.6.7.8.9])
list(r)
Copy the code
[1, 4, 9, 16, 25, 36, 49, 64, 81]
Copy the code
list(map(str[1.2.3.4.5.6.7.8.9]))
Copy the code
['1', '2', '3', '4', '5', '6', '7', '8', '9']
Copy the code
reduce
The first element is evaluated with the second element, the result is evaluated with the third element, and so on
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
Copy the code
from functools import reduce
Copy the code
def add(x, y) :
return x + y
Copy the code
reduce(add, [1.3.5.7.9])
Copy the code
25
Copy the code
def fn(x, y) :
return x * 10 + y
Copy the code
reduce(fn,[1.2.3.4])
Copy the code
1234
Copy the code
filter
Receive a function and a sequence
Filter () applies the passed function to each element in turn
Whether to keep or discard the element depends on whether the return value is True or False.
# a function that determines whether it is odd
def is_odd(n) :
return n % 2= =1
list(filter(is_odd, [1.2.4.5.6.9.10.15]))
Copy the code
[1, 5, 9, 15]
Copy the code
def not_empty(s) :
return s and s.strip()
list(filter(not_empty, ['A'.' '.'B'.None.'C'.' ']))
Copy the code
['A', 'B', 'C']
Copy the code
sorted
Take a sequence and a function
The function is not Boolean it’s used to process each element of the sequence
sorted([36.5, -12.9, -21], key=abs)
Copy the code
[5, 9, -12, -21, 36]
Copy the code
sorted(['bob'.'about'.'Zoo'.'Credit'])Sort for ASCII by default
Copy the code
['Credit', 'Zoo', 'about', 'bob']
Copy the code
sorted(['bob'.'about'.'Zoo'.'Credit'], key=str.lower)
Copy the code
['about', 'bob', 'Credit', 'Zoo']
Copy the code
sorted(['bob'.'about'.'Zoo'.'Credit'], key=str.lower, reverse=True)# Reverse the result
Copy the code
['Zoo', 'Credit', 'bob', 'about']
Copy the code
Lambda expressions
# Normal function writing
def f1(x) :
return x * x
Copy the code
#lambda expression
f2 = lambda x:x*x
Copy the code
print(f1(10))
print(f2(20))
Copy the code
100, 400,Copy the code
A decorator
Similar to spring’s AOP capabilities in Java
The __name__ attribute of the function object
def fun1() :
print("it's fun1")
Copy the code
f = fun1
f()
fun1()The effect is the same
Copy the code
it's fun1
it's fun1
Copy the code
print(f.__name__)
print(fun1.__name__)The ## function's __name__ attribute is the name of the function when it is defined, not the name of the variable assigned
Copy the code
fun1
fun1
Copy the code
Define decorator
Defining a decorator is defining a function
The argument passed to this function is a function
Define a new function inside the function that calls the arguments of the outside decorator function.
The result returned is the new function you just defined
The most common decorator
def log(func) :
def wrapper(*args, **kw) :
print('Before call')
print('call %s():' % func.__name__)
result = func(*args, **kw)
print('After call')
return result
return wrapper
Copy the code
Decorator with text added
Add a layer of functions to the normal decorator
The argument passed in is text
Returns the result as a decorator
def log3(text) :
def decorator(func) :
def wrapper(*args, **kw) :
print('This is log3 decorator')
return func(*args, **kw)
return wrapper
return decorator
Copy the code
Use decorators
Add @+ decorator names to functions that need decorators
@log
def fun2() :
print("it's fun2")
Copy the code
fun2()
Copy the code
Call fun2() before call: it's after call fun2Copy the code
@log3('fun3 decorator')
def fun3() :
print("it's fun3")
Copy the code
fun3()
Copy the code
This is the log3 decorator it's fun3Copy the code
The strange thing is that the name of the fun3 function has changed after using the decorator
fun3.__name__
Copy the code
'wrapper'
Copy the code
A way to keep the decorator definition of the __name__ attribute unchanged
Decorator with no parameters
import functools
Define the decorator
def log4(func) :
@functools.wraps(func)This is where the function's __name__ attribute is configured
def wrapper(*args, **kw) :
print('Before call')
print('call %s():' % func.__name__)
result = func(*args, **kw)
print('After call')
return result
return wrapper
@log4
def fun4() :
print("it's fun4")
Copy the code
print(fun4.__name__)# Finally not wrap
Copy the code
fun4
Copy the code
Decorator with parameters
Define the decorator
def log5(text) :
def decorator(func) :
@functools.wraps(func)This is where the function's __name__ attribute is configured
def wrapper(*args, **kw) :
print('Before call')
print('call %s():' % func.__name__)
result = func(*args, **kw)
print('After call')
return result
return wrapper
return decorator
@log5('this is just text of log5')
def fun5() :
print("it's fun5")
Copy the code
fun5.__name__
Copy the code
'fun5'
Copy the code
Partial function
It’s a new function generated after fixing a variable
This is implemented using the functools.partial function, which takes three arguments: the original function, the position argument, and the keyword argument
This thing I feel useless, I can write a function to achieve this effect, right
See: partial functions