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

Jonas Wielicki j.wielicki at sotecware.net
Thu Oct 30 09:23:58 CET 2014


(only referring to the second use case here)

On 30.10.2014 09:07, Antony Lee wrote:
> [snip]
> - In Qt programs (although I assume similar ideas apply to other GUI
> toolkits), where functions that operate on the GUI can only be called from
> the main thread, but "signals" (in the Qt sense of the term) can be used to
> communicate between threads, I find it useful to define a "call this in the
> GUI thread" generic signal.  The implementation is essentially
> 
> class Main(QMainWindow):
>     any_callable_signal = pyqtSignal(object)
>     def __init__(self, *args, **kwargs):
>         <...>
>         self.any_callable_signal.connect(lambda f: f()) # <- operator.call
> seems to express this better.
>     def call_in_gui_thread(self, func):
>         self.any_callable_signal.emit(func)
> 

How is that different from directly passing f? Or use functools.partial
in case you need to pass additional, fixed arguments to f.

regards,
jwi

> Antony
> 
> 2014-10-29 17:57 GMT-07:00 Steven D'Aprano <steve at pearwood.info>:
> 
>> 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
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
> 
> 
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
> 


More information about the Python-ideas mailing list