简体   繁体   English

我可以有条件地改变我正在呼叫的功能吗?

[英]Can I conditionally change which function I'm calling?

Sorry if this has been asked before, but I couldn't find the words to search that would give me the answer I'm looking for. 对不起,如果以前曾经问过,但是我找不到要搜索的字样,这会给我一些我正在寻找的答案。

I'm writing a script that contains a helper function, which, in turn, can call one of several functions which all take the same parameters. 我正在编写一个包含辅助函数的脚本,该函数反过来可以调用几个函数中的一个函数,这些函数都采用相同的参数。 In this helper function, I end up having strings of this: 在这个辅助函数中,我最终得到了这样的字符串:

if funcName="func1":
  func1(p1, p2, p3)
elif funcName="func2":
  func2(p1, p2, p3)
elif funcName="func3":
  func3(p1, p2, p3)
...

I know that I could use another helper function that would also take the funcName string and distribute it to the appropriate function, but is there a better way to do it? 我知道我可以使用另一个辅助函数,它也可以获取funcName字符串并将其分配给相应的函数,但是有更好的方法吗? What my brain wants to do is this: 我的大脑想要做的是:

funcName(p1, p2, p3)

That way I could call an arbitrary function name if I want to. 这样我可以调用任意函数名称。 Is something like that possible? 有可能吗?

Yes, you can, by using a dict mapping names to functions: 是的,你可以通过使用dict映射名称来实现函数:

funcs = dict(
    func1=func1,
    func2=func2,
    func3=func3
)

funcs[funcName](p1, p2, p3)

If you put the functions inside an object like so: 如果将函数放在一个对象中,如下所示:

var funcs = 
{
    f1: function() {},
    f2: function() {},
    f3: function() {}
}

You can do funcs['f1']() for example. 你可以做funcs['f1']()例如。

You can call them within the locals or globals dict: 你可以在localsglobals dict中调用它们:

locals()[funcName](p1, p2, p3)

Example: 例:

>>> a = lambda x: x+1
>>> locals()['a'](2)
3

As commented below you may check if they are within expected: 如下所述,您可以检查它们是否符合预期:

if funcName in ["func1", "func2", "func3"]:
    locals()[funcName](p1, p2, p3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在 python 中有一个方法/函数,我试图在其中获取一个返回值,然后我可以将其用作另一个函数中的变量 - I have a method / function in python in which i'm trying to get a return value which i can then use as a variable in another function 如何确定哪个函数负责调用%prun列出的函数? - How can I determine which function is responsible for calling a function listed by %prun? 我在从其他文件调用 function 时遇到问题 - I'm having a problem calling a function from a different file 我两次调用一个函数,但仅在第一次时有效 - I'm calling a function twice, but it works only the first time 如何在 Python 中实现一个有条件地充当上下文管理器的函数? - How can I implement a function in Python that conditionally behaves as a context manager? 为什么我可以访问在 Python 中的 function 之外有条件定义的变量? - Why can I access variables that are conditionally defined outside of function in Python? 如何判断我正在使用哪个 python 实现? - How can I tell which python implementation I'm using? 使用 a1.small 实例调用 SageMaker deploy_endpoint function 时,出现错误,提示我无法打开 m5.xlarge 实例 - When calling a SageMaker deploy_endpoint function with an a1.small instance, I'm given an error that I can't open a m5.xlarge instance 龟填充颜色-调用函数时我试图选择颜色,以便将来可以轻松更改它 - Turtle Filling Colors - I'm trying to choose the color when I call the function, so that I can easily change it in the future 我可以有条件地更改列表吗? - Can I Mutate a List Conditionally?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM