[Python-ideas] operator.call / operator.__call__

Steven D'Aprano steve at pearwood.info
Thu Oct 30 01:57:05 CET 2014


On Wed, Oct 29, 2014 at 03:39:59PM -0700, Antony Lee wrote:
> A simple suggestion: add "operator.call" and "operator.__call__", which
> would provide a function like Python2's "apply" (but it'd be called with
> already unpacked arguments, i.e. operator.call(f, *args, **kwargs)).
> Why?  To be able to pass it as an argument to other function, just like the
> other functions defined in the "operator" module.

I think you want something like this?

def call(callable, *args, **kwargs):
    return callable(*args, **kwargs)


Can you give an example of how you might use this call method? It 
doesn't have to be a real world use-case, just an illustration.

Perhaps I'm being a bit dim-witted today, but I'm having trouble 
thinking of where I would use this operator.call rather than just 
directly applying *args, **kwargs to the callable.


-- 
Steven


More information about the Python-ideas mailing list