我正在使用Python 2.7。有没有办法将名称、描述以及可能的其他元数据与函数关联起来,这样我就不必使用外部字典来保存这些信息,如下面的代码:
def predicateA(v):
# some logic that eventually returns a True/False value
return True
def predicateB(v):
# some logic that eventually returns a True/False value
return False
predicateNames={}
predicateNames[predicateA]='name and description for predicateA'
predicateNames[predicateB]='name and description for predicateB'
predicates = [predicateA, predicateB]
def evalForAllPredicates(v):
for f in predicates:
result = f(v)
print "function %s returned: %s" % (predicateNames[f], result)
evalForAllPredicates(3)