python partial, how to assign position of input argument
时间: 2024-02-25 08:55:38 浏览: 57
`functools.partial` in Python allows you to create a new function by partially applying arguments to an existing function. You can also assign the position of the input argument using `functools.partial`.
To assign the position of the input argument, you need to use the `functools.partial` function and pass the argument position as a keyword argument. Here is an example:
```python
import functools
def multiply(x, y):
return x * y
double = functools.partial(multiply, y=2) # Assigns the second argument's position to y
print(double(5)) # Output: 10
```
In the above example, we create a new function `double` from the `multiply` function by partially applying the `y` argument with a value of 2. We also assign the position of the `y` argument as the second argument of `multiply`.
Then, we call the `double` function with `x=5` as the first argument and `y=2` as the second argument. The output is `10`, which is the result of `5 * 2`.
阅读全文