如何在函数中解压缩变量数量参数(*args)的元组?



我试过了:

def df(t,x,*system_constants):
a,b,c,d,e,f,g,h,i,j,k,l,m = system_constants #unpack system constants
#algebra with these constants to calculate something
return something

调用时会返回一个错误,如下所示:"ValueError:没有足够的值来解包(预期为13,结果为0).

另外(不管这个错误),使用带有for循环的简短解包可能会更好。这个for循环是什么样的呢?

def df(t,x,*system_constants):
for arg in system_constants:
arg = system_constants
#algebra with these constants to calculate something
return something

这里唯一的目标是传递一个元组给一个函数而不会得到上面的错误,这样我就可以使用参数而不调用索引(即不转换为列表)

函数作为函数的参数被调用。solve_ivp积分器:

sol = solve_ivp(df, [t0,tf], x0, dense_output = True)

system_constants被简单地存储在同一个脚本中,就像:

system_constants = a,b,c,d,e,f,g,h,i,j,k,l,m

根据文档,

主叫签名为fun(t, y)。

函数只调用了2个参数,你期望的是15个。那不行。

最新更新