Non penso che ci sia un modo pythonic per risolvere la domanda. Ma nel mio codice è piuttosto una situazione comune, così ho scritto la mia propria funzione per questo:
def applyfs(funcs, args):
"""
Applies several functions to single set of arguments. This function takes
a list of functions, applies each to given arguments, and returns the list
of obtained results. For example:
>>> from operator import add, sub, mul
>>> list(applyfs([add, sub, mul], (10, 2)))
[12, 8, 20]
:param funcs: List of functions.
:param args: List or tuple of arguments to apply to each function.
:return: List of results, returned by each of `funcs`.
"""
return map(lambda f: f(*args), funcs)
Nel tuo caso avrei usato il seguente modo:
applyfs([mean, std, var, fxn4 ...], mylist)
Nota che davvero non è necessario utilizzare la funzione nomi (come si farebbe, ad esempio, in PHP4), in Python la funzione è l'oggetto chiamabile da solo e può essere memorizzata in un elenco.
EDIT:
O forse, sarebbe più divinatorio di utilizzare l'elenco di comprensione, invece di map
:
results = [f(mylist) for f in [mean, std, var, fxn4 ...]]
risposta eccellente, con voto in su. Nel caso fosse necessario verificare che il nome fornito si riferisca ad un add calleble 'if hasattr (func, '__call __')' alla list comprehension (usa 'callable (func)' su python 3) – Pynchia