python函数局部变量
什么是Python局部函数? (What is a Python Partial Function?)
Sometimes a function accepts multiple parameters. If there is a situation where we are passing the same parameter to it many times, we can create a python partial function. In the partial function, some of the parameters are fixed. It’s useful in creating a more meaningful function name from an existing function.
有时一个函数接受多个参数。 如果存在多次将相同参数传递给它的情况,我们可以创建一个python局部函数。 在部分功能中,某些参数是固定的。 从现有函数创建更有意义的函数名称时非常有用。
如何在Python中创建部分函数? (How to Create Partial Function in Python?)
Python functools partial() function is used to create a partial function. The functools module is for higher order functions. The partial() function syntax is:
Python functoolspartial()函数用于创建局部函数。 functools模块用于高阶函数。 partial()函数的语法为:
partial(func, /, *args, **keywords)
It creates a partial function that behaves like calling “func” with fixed positional and keyword arguments. We only need to pass a few of the arguments required to call the underlying function. The other arguments are already provided in the *args and **kwargs.
它创建了一个局部函数,其行为类似于使用固定的位置和关键字参数调用“ func ”。 我们只需要传递一些调用底层函数所需的参数即可。 其他参数已在* args和** kwargs中提供。
Python局部函数示例 (Python Partial Function Example)
Let’s say we have a multiply function like this:
假设我们有一个像这样的乘法函数:
def multiply(x, y):
print(f'Arguments: {x}, {y}')
return x * y
We want to utilize this function to multiply a list of integers with 4 and 5.
我们想利用此函数将整数列表与4和5相乘。
int_list = [1, 2, 3, 4]
int_list_4x = []
for i in int_list:
int_list_4x.append(multiply(i, 4))
print(int_list_4x)
int_list_5x = []
for i in int_list:
int_list_5x.append(multiply(i, 5))
print(int_list_5x)
Output:
输出:
Arguments: 1, 4
Arguments: 2, 4
Arguments: 3, 4
Arguments: 4, 4
[4, 8, 12, 16]
Arguments: 1, 5
Arguments: 2, 5
Arguments: 3, 5
Arguments: 4, 5
[5, 10, 15, 20]
We can create a partial function here to multiply an integer with 4 and 5.
我们可以在此处创建偏函数,以将整数与4和5相乘。
times_4 = partial(multiply, 4)
times_5 = partial(multiply, 5)
Now, the updated code to multiply the list of integers with 4 and 5 will be like this:
现在,将整数列表乘以4和5的更新代码将如下所示:
int_list_4x = []
for i in int_list:
int_list_4x.append(times_4(i))
print(int_list_4x)
int_list_5x = []
for i in int_list:
int_list_5x.append(times_5(i))
print(int_list_5x)
We can further reduce the code size using list comprehension.
我们可以使用列表理解来进一步减少代码大小。
int_list_4x = [times_4(x) for x in int_list]
int_list_5x = [times_5(x) for x in int_list]
The output, in this case, will be like this:
在这种情况下,输出将如下所示:
Arguments: 4, 1
Arguments: 4, 2
Arguments: 4, 3
Arguments: 4, 4
[4, 8, 12, 16]
Arguments: 5, 1
Arguments: 5, 2
Arguments: 5, 3
Arguments: 5, 4
[5, 10, 15, 20]
具有关键字参数的Python部分函数 (Python Partial Function with keyword arguments)
If you look at the above output, the positional argument of the partial function is always passed as the first parameter. In this case, it doesn’t matter. But, if the position of the partial function arguments matter, then we can use keyword arguments.
如果您看上面的输出,部分函数的位置参数始终作为第一个参数传递。 在这种情况下,没关系。 但是,如果部分函数参数的位置很重要,则可以使用关键字参数。
times_4 = partial(multiply, y=4)
int_list = [10, 20, 30]
int_list_4x = [times_4(x) for x in int_list]
print(int_list_4x)
Output:
输出:
Arguments: 10, 4
Arguments: 20, 4
Arguments: 30, 4
[40, 80, 120]
结论 (Conclusion)
Python partial functions are useful in creating a separate function when we call a function multiple times with some argument being the same all the time.
当我们多次调用某个函数且某个参数始终相同时,Python局部函数对于创建单独的函数很有用。
参考资料 (References)
翻译自: https://www.journaldev.com/33505/python-partial-function-functools-partial
python函数局部变量