【发布时间】:2018-07-12 01:14:36
【问题描述】:
我可以调用很多可能的函数,但我并不总是需要调用每个函数。
有没有一种方法可以传递我想调用的函数列表,然后动态调用它们?
换句话说:
def GetDate():
..does stuff..
return Date
def GetTime():
..does stuff..
return Time
def GetLocation():
..does stuff..
return Location
List_of_functions_to_call = ["GetDate","GetTime"]
def Call_functions(List = List_of_Functions_to_call):
for i in List:
...????...
..# Function would then call GetDate() and GetTime()
【问题讨论】:
-
怎么样...
i()?请注意,像列表这样的可变默认参数是个坏主意:stackoverflow.com/q/1132941/3001761 -
我同意为此目的列出一个列表不是一个好主意,我只是想解释我的问题。
-
函数是 Python 中的第一类对象,你可以通过
[GetDate, GetTime],然后i()调用它们。
标签: python python-2.7 function dynamic