This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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

Created on 2016-08-23 14:34 by vstinner, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
partial_call_kwargs.patch vstinner, 2016-08-23 14:34 review
Pull Requests
URL Status Linked Edit
PR 253 vstinner, 2017-02-23 15:40
Messages (6)
msg273456 - (view) Author: STINNER Victor (vstinner) * (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?

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

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 (vstinner) * (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 (vstinner) * (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:
msg288639 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-02-27 12:36
PR 253 has been merged.
Date User Action Args
2022-04-11 14:58:35adminsetgithub: 72027
2017-02-27 12:36:56berker.peksagsetstatus: open -> closed

nosy: + berker.peksag
messages: + msg288639

resolution: rejected
2017-02-23 15:40:30vstinnersetstatus: 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:20vstinnersetmessages: + msg273697
2016-08-26 03:27:51rhettingersetmessages: + msg273691
2016-08-24 20:36:25pitrousetnosy: + pitrou
messages: + msg273592
2016-08-23 14:34:03vstinnercreate