Function caller in operator module
Most operations that are available using operators in Python are provided by the operator module, but calling functions is noticeably absent is calling functions. A user can slightly abuse operator.methodcaller('__call__', ...) to perform this operation, but that is far from ideal. Should operator grow a new function caller, such that operator.caller(*args, **kwargs)(f) returns f(*args, **kwargs)? I have never personally needed this exact operation; it would be somewhat odd but not altogether unthinkable to sort a list of callables by their return values. The closes thing to this I have seen in the wild is map(apply, fs), which is limited to the no-arguments case. It is also possibly uglier than [f() for f in fs]. In any event, it's not even an option in Python 3.x.
On 3/3/2010 2:48 PM, Mike Graham wrote:
Most operations that are available using operators in Python are provided by the operator module, but calling functions is noticeably absent is calling functions. A user can slightly abuse operator.methodcaller('__call__', ...) to perform this operation, but that is far from ideal.
Should operator grow a new function caller, such that operator.caller(*args, **kwargs)(f) returns f(*args, **kwargs)?
Use case?
I have never personally needed this exact operation; it would be somewhat odd but not altogether unthinkable to sort a list of callables by their return values. The closes thing to this I have seen in the wild is map(apply, fs), which is limited to the no-arguments case. It is also possibly uglier than [f() for f in fs]. In any event, it's not even an option in Python 3.x.
And not needed, as you show. tjr
participants (2)
-
Mike Graham
-
Terry Reedy