假设我将函数调用放入列表中,
['next_release()', 'last_release("com_disagg")']
希望得到以下
的等效结果我怎么能用Python获取它
迭代数组并使用params动态调用函数。
next_release()
last_release("com_disagg")
为每个函数调用head()方法
next_release().head()
last_release("com_disagg").head()
为每个功能添加打印
print next_release().head()
print last_release("com_disagg").head()
答案 0 :(得分:1)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
var test = dictionary["time"] as! String
loadedItem.time = dateFormatter.dateFromString(test)
println("Time recorded as: \(loadedItem.time)")
println("Time from server: " + test)
输出:
Time recorded as: nil
Time from server: 2015-07-07 21:46:11
Time recorded as: nil
Time from server: 2015-07-07 21:58:09
Time recorded as: nil
Time from server: 2015-07-07 22:02:35
Time recorded as: nil
Time from server: 2015-07-07 23:46:49
答案 1 :(得分:1)
目前,您正在存储表示函数的字符串。这可能不是你想要的,因为它会强迫你使用通常不好的eval
。
如果必须使用字符串,则可以使用映射:
mapping = {
'next_release()': next_release(),
'last_release("com_disagg")': last_release("com_disagg")
}
现在您可以使用映射(mapping["next_release()"]
...)来获取结果。
使用实际函数而不是字符串有两个更好的选项:
如果您希望列表存储功能'结果(在调用它们之后):
functions_results = [next_release(), last_release("com_disagg")]
list_of_functions_heads = [x.head() for x in list_of_functions_results]
如果您希望列表存储函数'引用(请注意我在lambda
中使用参数关闭了函数以创建无参数函数:
functions_references = [next_release, lambda: last_release("com_disagg")]
list_of_functions_heads = [x().head() for x in list_of_functions_results]
答案 2 :(得分:0)
你可以使用clsoure:
arr = ['next_release()', 'last_release("com_disagg")']
def next_release():
def head():
return x;
def last_release(var):
def head():
return y;
for i in arr:
print i.head()