调用可调用对象列表

时间:2018-03-03 15:31:20

标签: python

假设我有一个可调用对象列表:

[<Python.Proto object at 0x1046ff940>,
<Database.Proto object at 0x10540c518>]

如何循环执行每个callable的列表,将结果保留在列表中?

2 个答案:

答案 0 :(得分:2)

这很简单,list comprehension喜欢:

int idThatYouWantToCheck = 12345; // here you can put any ID you're looking for
int indexInTheList = -1; // initialize with negative value, if after the for loop it becomes >=, matching ID was found

for (int i = 0; i < list.size(); i++) {
    if (list.get(i).getId == idThatYouWantToCheck) {
        indexInTheList = i;
        break;
    } 
}

答案 1 :(得分:0)

def foo():
   pass

def bar():
   pass

l = [str, map, foo, bar]
result = [call() for call in l]

您必须确保要将哪些参数传递给每个函数,您可以将其作为dict