简体   繁体   English

Python:如何以灵活的参数顺序调用函数?

[英]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.

相关问题 python中如何通过参数调用class function - How to call class function by parameter in python 如何调用cython cdef()函数,该函数包含python中的函数作为参数? - How to call a cython cdef() function that contains as a parameter a function in python? 如何通过Python在其他函数中使用参数调用一个函数? - How to call one function with parameter in other function by Python? python函数调用的参数顺序 - order of parameters of function call of python 如何在python中使用从CMD传递的参数调用定义的函数 - How to call defined function using Parameter passed from CMD in python 如何从 Perl 调用命名参数 Python function? - How do I call a named parameter Python function from Perl? 如何调用将 javascript 值作为参数传递给 python function - How to call pass javascript value as parameter to python function 如何将列表作为参数函数传递给 Python 3.2.3 中的 timeit 调用? - How to pass a list as parameter function into a timeit call in Python 3.2.3? python,如何在self.response.out.write()中作为参数调用函数 - python, how to call function in as parameter in self.response.out.write() rpy2:如何使用 python flask 中的参数调用 function? - rpy2: how to call function with parameter in python flask restful?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM