Skip to content
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

Add apply #411

Merged
merged 7 commits into from Jun 22, 2019
Merged

Add apply #411

merged 7 commits into from Jun 22, 2019

Conversation

eliasmistler
Copy link
Contributor

No description provided.

@llllllllll
Copy link
Contributor

I am +1 because this is generally useful and is duplicated in a lot of code; however, I somewhat object to the phrase "clojure-like": https://docs.python.org/2/library/functions.html#apply. Still very Pythonic.

@eriknw
Copy link
Member

eriknw commented Nov 13, 2018

Yeah, I agree this is common enough to justify being added.

It's unfortunate that apply is no longer anywhere to be found in core Python. The fatal flaw of the given implementation is that func= may not be used as a keyword.

What about something like this:

def apply(*func_and_args, **kwargs):
    if not func_and_args:
        raise TypeError('func argument is required')
    return func_and_args[0](*func_and_args[1:], **kwargs)

@eliasmistler
Copy link
Contributor Author

@eriknw Agree and updated

@eliasmistler eliasmistler changed the title Add a Clojure-style apply function Add an apply function Nov 23, 2018
@eliasmistler eliasmistler changed the title Add an apply function Add apply Nov 23, 2018
@eliasmistler
Copy link
Contributor Author

@eriknw ready to merge as far as I'm concerned

@eriknw eriknw merged commit 639043e into pytoolz:master Jun 22, 2019
@eriknw
Copy link
Member

eriknw commented Jun 22, 2019

This is in, thanks @eliasmistler! (and sorry for the delay)

@Hugovdberg
Copy link

Just a remark perhaps for future reference, as of Python 3.8 the signature could be simplified once more by use of the positional only indicator:

def apply(func, /, *args, **kwargs):
    return func(*args, **kwargs)

def test_no_keyword_clash(x, func):
    return x+func

apply(test_no_keyword_clash, x=1, func=2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants