gpt4 book ai didi

python - Python中通过数组索引调用函数

转载 作者:IT老高 更新时间:2023-10-28 21:17:28 24 4
gpt4 key购买 nike

我在 Python out1、out2、out3 等中有一堆函数,我想根据我传入的整数调用它们。

def arryofPointersToFns (value):
#call outn where n = value

有没有简单的方法可以做到这一点?

最佳答案

tl;dr: 写一个 out(n) 函数而不是 out1(), out2(), ..., outN()有了这个 hack。

我无法想象在实践中会出现这个问题的合理场景。请重新考虑问题的架构;可能有更好的方法来做到这一点(因为将它们存储在列表中意味着除了索引之外的函数没有任何意义;例如,我只能想象如果你正在创建你想要这样做一堆动态生成的 thunk,它们的时间顺序很重要,或者类似的东西)。尤其是您正在阅读此答案的任何新手用户,请考虑制作一个可以处理所有内容的更通用的函数,或者为每个函数关联更多的识别信息,或者将其作为类的一部分等等。

也就是说,这就是你的做法。

myFuncs = [f0,f1,f2]
myFuncs[2](...) #calls f2

myFuncs = {'alice':f1, 'bob':f2}
myFuncs['alice'](...) #calls f1

这只是以下两步合二为一:

myFuncs = [f0,f1,f2]
f = myFuncs[i]
f(...) #calls fi

或者,如果您没有像上面所说的 OP 那样的函数 'myFunc' 的注册表,您可以使用 globals(),尽管它是一种非常骇人听闻的形式并且应该避免(除非您希望这些函数在你的模块命名空间,在这种情况下可能没问题......但这可能很少见,你可能宁愿在子模块中定义这些函数,然后 from mysubmodule import * 它们,它在微微皱眉):

def fN(n):
return globals()['f'+str(n)]

def f2():
print("2 was called!")

fN(2)(...) #calls f2

这里有两个其他想法(在接受答案和前两条评论后添加):

你也可以像这样创建一个装饰器:

>>> def makeRegistrar():
... registry = {}
... def registrar(func):
... registry[func.__name__] = func
... return func # normally a decorator returns a wrapped function,
... # but here we return func unmodified, after registering it
... registrar.all = registry
... return registrar

并像这样使用它:

>>> reg = makeRegistrar()
>>> @reg
... def f1(a):
... return a+1
...
>>> @reg
... def f2(a,b):
... return a+b
...
>>> reg.all
{'f1': <function f1 at 0x7fc24c381958>, 'f2': <function f2 at 0x7fc24c3819e0>}

然后你可以调用 reg.all['f1']。您可以调整 reg 装饰器以跟踪索引并执行以下操作:

registry = []
index = int(re.regextofindthenumber(func.__name__))
if not index==len(registry):
raise Exception('Expected def f{} but got def f{}')
else:
registry[index] = func

或者,为了避免 globals(),您可以定义一个类:

class Funcs(object):
def f1():
...
def f2():
...
def num(n):
[code goes here]

如果您的函数数量很少,您可以使用 ['f1','f2','f3'][i]

当然,没有更多信息,所有这些建议都只是忽略了真正的问题:这种情况永远不应该出现,并且可能是严重架构缺陷的迹象,而您可能更愿意拥有一些东西(使用您的示例)喜欢:

# a possibly-better world
def out(n):
# output to N, whatever that means

而不是

# what you have now
def out1():
# output to 1
def out2():
# output to 2
def outN(n):
# ???

关于python - Python中通过数组索引调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5707589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com