[英]python: how to call a function with a flexible parameter order?
Let us take an example. 让我们举个例子。 In certain libraries like "scipy.integrate" calling a function like "odeint" (integrating functions) has to be expressed as "odeint(func, y0, T, ...)" where "func" is a name of a function that has to be predefined with two parameters (y,t0), y vector and t0 a scalar. 在某些库中,例如“ scipy.integrate”,调用诸如“ odeint”(集成函数)之类的函数必须表示为“ odeint(func,y0,T,...)”,其中“ func”是该函数的名称必须使用两个参数(y,t0),y向量和t0标量进行预定义。
The question is how to use "odeint" if the already defined function "func" is specified with two parameters but in the inverse order "(t0, y)". 问题是,如果已经定义的函数“ func”用两个参数但以相反的顺序“(t0,y)”指定,则如何使用“ odeint”。
Best regards. 最好的祝福。
You can use a lambda function to reorder the arguments like so: 您可以使用lambda函数重新排列参数,如下所示:
odeint(lambda p, q: func(q, p), y0, T, ...)
Alternatively, you can supposedly swap the orders in the main call if all odeint
does is call func
on the arguments and does interact directly with the arguments: 另外,如果所有odeint
所做的只是在参数上调用func
并且确实与参数交互,则可以在主调用中交换订单:
odeint(func, T, y0, ...)
You can explicitly call parameters in arbitrary sequence: 您可以按任意顺序显式调用参数:
def func(a, b, c):
print('a = {}'.format(a))
print('b = {}'.format(b))
print('c = {}'.format(c))
x = 1
y = 2
z = 3
func(a = x, b = y, c = z)
func(c = z, b = y, a = x)
func(b = y, c = z, a = x)
PS I'm not on 100% sure, but try odeint(func, y0 = y0, T = T, ...)
PS我不是100%确定,但尝试odeint(func, y0 = y0, T = T, ...)
or odeint(func, T = T, y0 = y0, ...)
. 或odeint(func, T = T, y0 = y0, ...)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.