我想从一个函数传递一个非可变整数参数给其他已定义的函数;我的错误是什么?

问题描述:

假设我有一个函数调用链。我想从一个函数传递一个非可变整数参数给其他已定义的函数;我的错误是什么?

def func1(pick, arg1, arg2): 
    if pick == 1: 
     do stuff with arg1 and arg2 
    elif pick == 2: 
     do other stuff with arg1 and arg2 
    return stuff that got done 

def func2(pick, arg1, arg2, arg3): 
    if pick == 1: 
     do stuff with arg1 and arg2 and arg3 
    elif pick == 2: 
     do other stuff with arg1 and arg2 and arg3 
    return stuff that got done 

def func3(pick, func2, arg3): 
    if pick == 1: 
     do stuff with funcs and arg3 
    elif pick == 2: 
     do other stuff with funcs and arg3 
    return stuff that done 
etc .. 

我能够经由SciPy的quad(数值积分)从一个功能通过参数传递给另一个以确保ARGS 不是可变的。我还能够通过SCIPY minimize(优化)将参数从一个功能传递到另一个功能,其中参数可变。我的麻烦是将不可变的输入pick从一个函数传递到另一个函数。如果我下print(pick)如在上面的简化示例中,每个定义的函数的第一行,并且如果我把这种链的功能

callme = func3(2 , func2(pick = pick, args) , [6, 0.5]) 

然后我的代码最终将吐出读取的错误消息

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

但它首先会做这样的事情:

1 
2 
1 
2 
2 # (from pick = 2 in func3) 
[6 0.5] 

如何/为什么会出现这种情况,是有可能发送输入pick从一个函数到另一个函数调用?

编辑:我的猜测是将pick作为类的对象传递,或者使用kwargs,或者将pick传递为由单个元素组成的不可压缩元组;但我不确定这是否正确或如何实施。理想情况下,我不知道的一些操作就像callme = func3(pick.func2 , func2, [6, 0.5])一样简单。我试过声明pickglobal,但这会导致有关参数为全局的错误消息。

我删除了pick作为来自每个函数的输入,但将其作为变量保留在函数中。然后我在函数链之前放置了下面的代码来初始化pick

def selectdist(pick): 
    ## 1 for original representation 
    ## 2 for normal representation on semilog(x) axis 
    ## 3 for normal representation on linearly-spaced ln(x) axis 
    return int(pick) 

pickdist = selectdist(3) # choose 1 2 or 3 

函数链后,一个使用运行函数链重新初始化pick。由于它不再是从顶部函数链向下传递的参数,所以错误的来源已经消失。