classification
Title: functools.partial: don't copy keywoard arguments in partial_call()?
Type: performance Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, haypo, pitrou, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2016-08-23 14:34 by haypo, last changed 2017-02-27 12:36 by berker.peksag. This issue is now closed.

Files
File name Uploaded Description Edit
partial_call_kwargs.patch haypo, 2016-08-23 14:34 review
Pull Requests
URL Status Linked Edit
PR 253 haypo, 2017-02-23 15:40
Messages (6)
msg273456 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2016-08-23 14:34
I justed optimized partial_call() for positional arguments in the change c1a698edfa1b to avoid the creation of a temporary tuple when possible.

I noticed that keyword parameters from partial() constructor are always copied. Is it mandatory? Can't we avoid copying them?

Example:
---
import functools
hello = functools.partial(print, "Hello World", end='!\n')
hello()
---

hello keyword arguments are {'end'; '!\n'}.

Attached patch avoids copying keyword arguments when the partial objects is not called with new keyword arguments.

Tests pass, but I don't know if there is a risk that some strange function modify keyword arguments in-place?
msg273592 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2016-08-24 20:36
Modifying the keyword arguments dict is quite common so this change would be a heavy compatibility breaker.

A well-known idiom:

  def some_function(..., **kwargs):
      some_option = kwargs.pop('some_option', None)
      # further process kwargs
msg273691 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-08-26 03:27
I concur with Antoine.
msg273697 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2016-08-26 09:36
Ah yes sorry, I misunderstood the link between kwargs from a C function and
a PY function. The link is simple, it's the same object :-) This issue can
be closed.
msg288454 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2017-02-23 15:40
I reopen the issue to propose to add a comment explaining why the dictionary must always be copied:
https://github.com/python/cpython/pull/253
msg288639 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-02-27 12:36
PR 253 has been merged.
History
Date User Action Args
2017-02-27 12:36:56berker.peksagsetstatus: open -> closed

nosy: + berker.peksag
messages: + msg288639

resolution: rejected
2017-02-23 15:40:30hayposetstatus: closed -> open
resolution: rejected -> (no value)
messages: + msg288454

pull_requests: + pull_request221
2016-08-26 13:31:38ned.deilysetstatus: open -> closed
resolution: rejected
stage: resolved
2016-08-26 09:36:20hayposetmessages: + msg273697
2016-08-26 03:27:51rhettingersetmessages: + msg273691
2016-08-24 20:36:25pitrousetnosy: + pitrou
messages: + msg273592
2016-08-23 14:34:03haypocreate