Issue
The Python 3.7.2 documentation on the asyncio eventloop says in the documentation of the call_later
function:
The optional positional args will be passed to the callback when it is called. If you want the callback to be called with keyword arguments use functools.partial().
Is using functools.partial
considered superior to using lambda
for this case?
Solution
Is using
functools.partial
considered superior to usinglambda
for this case?
"Superior" is too strong a word. It might be correct to say that functools.partial
is the "one obvious choice" for simple argument binding.
Some possible advantages of functools.partial
compared to lambda
not implied by the above sentence:
As pointed out by @AlexHall,
functools.partial
avoids the late binding mistake that often occurs when the lambda is created in a loop.functools.partial
might make the intent clearer to some readers. (This is obviously individual, as readers with any FP background will typically have a strong preference forlambda
.)In CPython
functools.partial
might be a tiny bit faster thanlambda
because it doesn't need to create a Python stack frame, nor does it need to perform the actual binding; its optimized C implementation just needs to execute the call on the object. The difference should be measured on a case-by-case basis.
Answered By - user4815162342
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.