3 techniques to simplify Python function calls

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it yourself

Python free learning materials and group communication answers Click to join


Suppose there is a function, this function needs to receive 4 parameters, and return the sum of these 4 parameters:

def sum_four(a, b, c, d):
  return a + b + c + d

If you need to fix the first three parameters and only change the value of the last parameter, you may need to call this time:

>>> a, b, c = 1, 2, 3

>>> sum_four(a=a, b=b, c=c, d=1)
7

>>> sum_four(a=a, b=b, c=c, d=2)
8

>>> sum_four(a=a, b=b, c=c, d=3)
9

>>> sum_four(a=a, b=b, c=c, d=4)
10

This is really ugly. If you use the Map function, can you simplify the code?
The answer is yes, but the Map function [generally] can only accept a single element. If you use it forcefully, it will report this error:

list(map(sum_four, [(1, 2, 3, 4)]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sum_four() missing 3 required positional arguments: 'b', 'c', and 'd'
怎么解决?

Solution 1: itertools.starmap

You can use the itertools function starmap to replace Map.
It is different from Map in that it allows accepting a tuple as the argument to sum_four.

>>> import itertools
>>> list(itertools.starmap(sum_four, [(1, 2, 3, 4)]))
[10]

Great, in this case, the above problems can be solved using the starmap function:

>>> import itertools

>>> ds = [1, 2, 3, 4]

>>> items = ((a, b, c, d) for d in ds)

>>> list(items)
 [(1, 2, 3, 1), (1, 2, 3, 2), (1, 2, 3, 3), (1, 2, 3, 4)]

>>> list(itertools.starmap(sum_four, items))
 [7, 8, 9, 10]

Please note that items is a generator. This is to avoid excessive memory consumption due to excessive items. Paying attention to these details during normal development can help you to open the gap with ordinary developers.

Option 2: functools.partial

The second solution is to use the partial function to fix the first three parameters.
According to the documentation, partial will "freeze" some parts of the function's parameters to generate a simplified version of the function.

So the solution to the above problem is:

>>> import functools
>>> partial_sum_four = functools.partial(sum_four, a, b, c)
>>> partial_sum_four(3)
9
>>> # 这样就可以使用map函数了:
>>> list(map(partial_sum_four, ds))
[7, 8, 9, 10]

Solution 3: itertools.repeat()

In fact, the Map function is allowed to pass iterable parameters, but it has an interesting feature. It uses the items in each iterable object as different parameters of the passed-in function. This may be too abstract, let's take a look at a practical example:

>>> list(map(sum_four, [1,1,1,1], [2,2,2,2], [3,3,3,3], [1,2,3,4]))
 [7, 8, 9, 10]

Obviously, each time the corresponding subscript items in different arrays are passed into the function for calculation.
In this way, we can use this feature to optimize.

The itertools.repeat() function can generate an iterator based on the parameters, and the iterator returns the object again and again. Without specifying the times parameter, it will run indefinitely.

The Map function will automatically stop running after the shortest iterable object is iterated.
Combining these two features, the solution to the above problem comes out:

>>> import itertools
>>> list(map(sum_four, itertools.repeat(a), itertools.repeat(b), itertools.repeat(c), ds))
 [7, 8, 9, 10]

This trick is very clever. The disadvantage is that not many people can understand it. But that’s okay, just know something in the computer world, you don’t necessarily need to use it.

For example, these solutions in this article are generally not used in daily life work, so you don’t need to memorize, but you need to know [there are such problems] and [there are these solutions], in case you encounter similar You can recall this article and quickly find a solution.

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/112937636