将变量和函数(具有不同参数)传递给函数
Passing variables and a function (with different arguments) to a function
我有一个外部函数 external_function
作为参数 x
、y
、step
和另一个函数 func
作为参数 x
、y
和(通常但不总是)其他变量。
我不知道如何传递这些附加参数。
我试过 *args
或 functools.partial
,但没有成功。可能是我没用对吧
这是一个代表我真实代码问题的例子:
def general_operation(x, y, step, func):
"""
func is a function which expects at least x & y as arguments,
but it can expect more arguments.
"""
number_of_iterations = int((y - x) / step)
solutions = []
for i in range(number_of_iterations):
solutions.append(func(x, y))
return solutions
def funza(x, y):
return x + y
def gunza(x, y, additional):
if additional == 0:
return x + y
elif additional == 1:
return x - y
elif additional == 2:
return x * y
elif additional == 3:
return x / y
x = 0
y = 100
step = 0.01
# works
solutions_1 = general_operation(x, y, step, funza)
# do not know how to pass "additional" argument here
solutions_2 = general_operation(x, y, gunza)
# do not know how to pass "additional" argument here
solutions_3 = general_operation(x, y, step, gunza)
print(solutions_1)
print(solutions_2)
print(solutions_3)
您可以使用 *args
和 **kwargs
:
def general_operation(x, y, step, func, *args, **kwargs):
"""
A function which expects at least x & y as arguments,
but it can expect more arguments.
"""
number_of_iterations = int((y - x) / step)
solutions = []
for i in range(number_of_iterations):
solutions.append(func(x, y, *args, **kwargs))
return solutions
此处 *args
将包含元组中的所有未命名参数,**kwargs
将包含字典中的所有命名参数。
您可以这样调用它:
solutions_1 = general_operation(x, y, step, funza)
solutions_2 = general_operation(x, y, step, gunza, 1)
func
之外的所有其他参数都将传递给 func
我有一个外部函数 external_function
作为参数 x
、y
、step
和另一个函数 func
作为参数 x
、y
和(通常但不总是)其他变量。
我不知道如何传递这些附加参数。
我试过 *args
或 functools.partial
,但没有成功。可能是我没用对吧
这是一个代表我真实代码问题的例子:
def general_operation(x, y, step, func):
"""
func is a function which expects at least x & y as arguments,
but it can expect more arguments.
"""
number_of_iterations = int((y - x) / step)
solutions = []
for i in range(number_of_iterations):
solutions.append(func(x, y))
return solutions
def funza(x, y):
return x + y
def gunza(x, y, additional):
if additional == 0:
return x + y
elif additional == 1:
return x - y
elif additional == 2:
return x * y
elif additional == 3:
return x / y
x = 0
y = 100
step = 0.01
# works
solutions_1 = general_operation(x, y, step, funza)
# do not know how to pass "additional" argument here
solutions_2 = general_operation(x, y, gunza)
# do not know how to pass "additional" argument here
solutions_3 = general_operation(x, y, step, gunza)
print(solutions_1)
print(solutions_2)
print(solutions_3)
您可以使用 *args
和 **kwargs
:
def general_operation(x, y, step, func, *args, **kwargs):
"""
A function which expects at least x & y as arguments,
but it can expect more arguments.
"""
number_of_iterations = int((y - x) / step)
solutions = []
for i in range(number_of_iterations):
solutions.append(func(x, y, *args, **kwargs))
return solutions
此处 *args
将包含元组中的所有未命名参数,**kwargs
将包含字典中的所有命名参数。
您可以这样调用它:
solutions_1 = general_operation(x, y, step, funza)
solutions_2 = general_operation(x, y, step, gunza, 1)
func
之外的所有其他参数都将传递给 func