<div dir="ltr"><div>I have two use cases below. Yes, it is true that just defining the "call" function myself would only take me two lines, but on the other hand the same is true for most functions in the operator module...</div><div><br></div><div>- Sometimes, depending on a switch, you want either to call a function directly, or to pass it to a specialized "caller", e.g.</div><div><br></div><div>def run_in_thread(func, *args, **kwargs):</div><div> Thread(target=func, args=args, kwargs=kwargs).start()</div><div><br></div><div>def run_here_or_in_thread(in_thread, func, *args, **kwargs):</div><div> (run_in_thread if in_thread else operator.call)(func, *args, **kwargs)</div><div><br></div><div>or to optionally use a ContextDecorator:</div><div><br></div><div>def run_in_ctx(ctx, func, *args, **kwargs):</div><div> (ctx or operator.call)(func)</div><div><br></div><div>- 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</div><div><br></div><div>class Main(QMainWindow):</div><div> any_callable_signal = pyqtSignal(object)</div><div> def __init__(self, *args, **kwargs):</div><div> <...></div><div> self.any_callable_signal.connect(lambda f: f()) # <- operator.call seems to express this better.</div><div> def call_in_gui_thread(self, func):</div><div> self.any_callable_signal.emit(func)</div><div><br></div><div>Antony</div></div><div class="gmail_extra"><br><div class="gmail_quote">2014-10-29 17:57 GMT-07:00 Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Wed, Oct 29, 2014 at 03:39:59PM -0700, Antony Lee wrote:<br>
> A simple suggestion: add "operator.call" and "operator.__call__", which<br>
> would provide a function like Python2's "apply" (but it'd be called with<br>
> already unpacked arguments, i.e. operator.call(f, *args, **kwargs)).<br>
> Why? To be able to pass it as an argument to other function, just like the<br>
> other functions defined in the "operator" module.<br>
<br>
</div></div>I think you want something like this?<br>
<br>
def call(callable, *args, **kwargs):<br>
return callable(*args, **kwargs)<br>
<br>
<br>
Can you give an example of how you might use this call method? It<br>
doesn't have to be a real world use-case, just an illustration.<br>
<br>
Perhaps I'm being a bit dim-witted today, but I'm having trouble<br>
thinking of where I would use this operator.call rather than just<br>
directly applying *args, **kwargs to the callable.<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
--<br>
Steven<br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br></div>