Python programming functools module to create modified high order function analysis

  • 2021-11-30 00:58:13
  • OfStack

Directory partial function decorator @ lru_cachereduce function

partial function

partial It is a partial function (also called partial application function in some places), which encapsulates the function twice, binding some parameters of the existing function to the specified value in advance, and then calculating it.

Because partial functions have few variable parameters, it is difficult to call functions.

Show the code directly:


from functools import partial
#  Primitive function declaration 
def show(name, level):
    print("name:", name, "level:", level)
#  Define partial function, encapsulate  show()  Function, and is the  name  Parameter sets the default parameter 
show_level = partial(show, name=' Eraser ')
#  Due to  name  Parameter already has a default value, when calling a partial function, name  You can not specify 
show_level(level="9 Grade ")

The above code is to use the partial Function, take some parameters of a function (in this case, name ) is fixed (equivalent to providing a default value), and then a new function is returned, and the parameters of the new function are reduced.

Another point is that the above code calls show_level Function, you must use the keyword parameter form to the level Perform a value pass, otherwise the TypeError Error, as follows:


#  The code is written as follows 
show_level("9 Grade ")
#  The exception is as follows 
TypeError: show() got multiple values for argument 'name'

Partial functions can also be implemented by anonymous functions, such as the following code:


#  The code is written as follows 
show_level("9 Grade ")
#  The exception is as follows 
TypeError: show() got multiple values for argument 'name'

Use timeit Running 100,000 times, the time of testing 1 and 2 is basically not much different, so it can be used interoperably, but anonymous functions still realize 1 relatively simple function.

Decorator @ lru_cache

Add to the function @lru_cache Decorator, which can speed up the operation of functions, lru Means that the most recently used calculation results will remain in the cache.

The prototype of the decorator is as follows:


@functools.lru_cache(maxsize=None, typed=False)

maxsize : The maximum number of caches, if None, there is no limit, set to 2n, the best performance;

partial0 If set to True (note that there is no such parameter in functools32), calls of different parameter types are cached separately, such as f (3) and f (3.0).

Next, through the recursion of Fibonaccet sequence, it shows whether there is or not lru_cache The difference.


from functools import lru_cache
import timeit
@lru_cache()
def factorial(n):
    return 1 if n <= 1 else n * factorial(n - 1)
a = timeit.timeit(stmt="factorial(20)", setup='from __main__ import factorial', number=100000)
print(a)
Without lru_cache Time consuming: 0.2; Tape time: 0.06

The difference is obvious, because every time you execute factorial The cache pool maintained by the decorator is checked, and if the value exists, the corresponding result is obtained directly to avoid double calculation.

The general conclusion is that for applications that need to calculate the same set of values repeatedly, use decorators @lru_cache Can greatly improve performance.

reduce function

reduce Function is also a high-order function, which can combine two adjacent values in an iterable object at 1 through a specified function, so sum , len , max , min Can be regarded as is reduce A special form of a function.

Definition of reduce function:

reduce(function, sequence [, initial] ) - > value

function参数 Is a function with two arguments, reduce Sequentially from sequence Fetch 1 element from the last call to the function The result of the call is called again as an argument function .

If you do not specify the first time initial Is used by default sequence The first element of is passed into 2 yuan with the next element 1 function Function to execute.

It's a bit confusing to read, just look at the case directly.


from functools import reduce
def add(x, y):
    return x + y
a = reduce(add, [1, 2, 3, 4])
print(a)

initial Parameter represents the initial value, which is the first value of the sequence by default.


from functools import reduce
a = reduce(lambda x, y: x + y, [1, 2, 3, 4], 2)
print(a)

Here's how to use the reduce Realization sum , len Equal functions.


from functools import reduce
data = [1, 2, 3, 4]
sum = lambda data: reduce(lambda x, y: x + y, data, 0)
count = lambda data: reduce(lambda x, y: x + 1, data, 0)
min = lambda data: reduce(lambda x, y: x if x < y else y, data)
a = sum(data)
b = count(data)
c = min(data)
print(a, b, c)

You can also use the reduce Function and partical Function realization sum Function, the code is as follows:


from functools import reduce, partial
data = [1, 2, 3, 4]
sum = partial(reduce, lambda x, y: x + y)
a = sum(data)
print(a)

The above is Python programming functools module to create a modified function of higher-order function analysis details, more about Python programming functools module to create a modified higher-order function information please pay attention to other related articles on this site!


Related articles: