Skip to content

Allow functools.reduces 'initial' to be a keyword argument #125916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sayandipdutta opened this issue Oct 24, 2024 · 2 comments · Fixed by #125917
Closed

Allow functools.reduces 'initial' to be a keyword argument #125916

sayandipdutta opened this issue Oct 24, 2024 · 2 comments · Fixed by #125917
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@sayandipdutta
Copy link
Contributor

sayandipdutta commented Oct 24, 2024

Feature or enhancement

Proposal:

functools.reduce takes function (generally a callable) and iterable, with an optional initial parameter:

https://docs.python.org/3/library/functools.html#functools.reduce

However, initial cannot be passed as a keyword argument, which reduces ;) readability,

from functools import reduce
from operator import sub
>>> reduce(sub, [1, 1, 2, 3, 5, 8], 21)
1
>>> reduce(sub, [1, 1, 2, 3, 5, 8], initial=21)
TypeError: reduce() takes no keyword arguments

Allowing initial as keyword argument will be more clear, and initial could also be passed as a keyword argument while making partial functions out of reduce.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/remove-positional-only-restriction-on-initial-parameter-of-reduce/68897

Linked PRs

@sayandipdutta sayandipdutta added the type-feature A feature request or enhancement label Oct 24, 2024
@skirpichev
Copy link
Member

See also #121677

@skirpichev
Copy link
Member

Also see #96640.

@sobolevn, did you actually try to do conversion and benchmark it?

skirpichev added a commit to skirpichev/cpython that referenced this issue Oct 26, 2024
@erlend-aasland erlend-aasland added the extension-modules C modules in the Modules dir label Nov 1, 2024
picnixz pushed a commit to picnixz/cpython that referenced this issue Dec 8, 2024
ebonnal pushed a commit to ebonnal/cpython that referenced this issue Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants