I am trying to "invert" map
(same function, multiple arguments) in a case where I have multiple function that I want to apply to the same argument. I am trying to find a more function approach to replace the classical
arg = "My fixed argument"
list_of_functions = [f, g, h] #note that they all have the same signature
[fun(arg) for fun in list_of_functions]
The only thing I could come up with is
map(lambda x: x(arg), list_of_functions)
which is not really great.
You can try:
The
operator
module also has similar functions for getting fixed attributes or items out of an object, often useful in functional-esque programming style. Nothing directly for calling a callable, butmethodcaller
is close enough.Though, I personally like list comprehension more in this case. Maybe if there was a direct equivalent in the
operator
module, like:…to use it as:
…then maybe it would be convenient enough?
In Python 3 your
map()
example returns a map object, so the functions are only called when it is iterated over, which is at least lazy.