我试图将一个不可变整数参数从一个函数传递给其他定义的函数;我的错误是什么? [英] I am trying to pass a non-mutable integer argument from one function to other defined functions; what is my mistake?

查看:192
本文介绍了我试图将一个不可变整数参数从一个函数传递给其他定义的函数;我的错误是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def func1(pick,arg1,arg2):
如果选择== 1:
使用arg1和arg2进行填充
elif pick == 2:
使用arg1和arg2执行其他操作
返回已完成的内容

def func2(pick,arg1,arg2,arg3):
如果pick == 1:
使用arg1和arg2和arg3进行填充
elif pick == 2:
用arg1和arg2和arg3做其他的东西
返回已完成的东西

def func3(pick,func2,arg3):
如果pick == 1:
使用funcs和arg3做东西
elif pick == 2:
用funcs和arg3做其他的东西
返回完成
等的东西

我能够通过SCIPY将一个函数的args传递给另一个函数 quad (数字积分),以确保args 不是可变。我也可以通过SCIY 最小化(优化)将args从一个函数传递给另一个函数,其中args 可变的。我的麻烦是将不可变的输入选择从一个函数传递给另一个函数。如果我将 print(pick)作为上述简化示例中的每个定义函数的第一行,并且如果我将这个函数链称为

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

然后我的代码最终会吐出一个错误消息,读取

  ValueError:具有多个元素的数组的真值是不明确的。使用a.any()或a.all()

但首先它会做一些像: / p>

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

如何/为什么会发生这种情况,是吗可能在一个函数链中将一个函数的输入 pick 发送给另一个函数?



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

解决方案

我从每个功能中删除了 pick 作为输入, functino中的一个变量。然后我在函数链之前放置以下代码来初始化 pick

  select selectdist(pick):
## 1用于原始表示
## 2用于半对数(x)轴上的正常表示
## 3用于线性间隔ln(x)上的正态表示)轴
返回int(pick)

pickdist = selectdist(3)#选择1 2或3

在功能链之后,一个使用重新初始化的选项运行功能链。由于它不再是从功能链顶部传递的参数,所以错误的来源已经消失了。


Suppose I have a chain of function calls.

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 ..

I was able to pass args from one function to another via SCIPY quad (numerical integration) so as to ensure that the args were not mutable. I was also able to pass args from one function to another via SCIPY minimize (optimization) in which the args are mutable. My trouble is in passing the non-mutable input pick from one function to another. If I were to place print(pick) as the first line of each defined function in the simplified example above, and if I were to call this chain of functions as

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

then my code would eventually spit out an error message that reads

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

but first it would do something like:

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

How/Why is this happening, and is it possible to send the input pick from one function to another in a chain of function calls?

Edit: My guess would be passing pick as an object of a class, or using kwargs, or passing pick as an unpackable tuple consisting of a single element; but I'm not sure if this is correct or how to implement this. Ideally, there is some operation I'm unaware of that is as simple as callme = func3( pick.func2 , func2, [6, 0.5] ). I've tried declaring pick as global, but this results in an error-message about a parameter being global.

解决方案

I removed pick as an input from every function but left it as a variable in the functino. I then placed the following code before the function chain to initialize 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

After the function chain, one run the function chain using a re-initialized pick. Since it no longer is an argument that is passed from the top of the function chain on down, the source of the bug is gone.

这篇关于我试图将一个不可变整数参数从一个函数传递给其他定义的函数;我的错误是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆