Live data from Hacker News

More intuitive partial function application in Python

github.com

11–20 of 36 posts

Re: More intuitive partial function application in Python

#11

Could this be used without the "bp."? Half the point of coming up with a nicer notation is for the notation to actually be nicer.

Well, you could do

from better_partial import partial, _

presumably. Though since _ has some meaning in Python maybe you'd want to do

from better_partial import partial, _ as x

instead (or something like it).

Re: More intuitive partial function application in Python

#13
Current solutions:

  >>> list(map(partial(pow, exp=3), range(10)))
  [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]

  >>> list(map(lambda base: pow(base, 3), range(10)))
  [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]

  >>> [pow(base, 3) for base in range(10)]
  [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
Proposed solution:

  list(map(bp.partial(pow)(bp._, 3), range(10))
ISTM the only time you come out a little ahead is if you know in advance that a function is going to be partialed (so you can decorate it), that you will need to partial arguments in other than a left to right fashion (otherwise a plain partial will suffice), that you won't use keyword arguments (the current partial works with keywords), and that you really dislike the lambda keyword (which neatly covers all cases from simple to complex without a new notation).

That said, this is a clever recipe. Kudos to the author for putting it together. I don't think it really rises to the level of "better" or "more intuitive", but it is an interesting experiment.

Re: More intuitive partial function application in Python

#14
I love the idea of using underscore to represent partial currying.

Somewhat related, here's a similar PR for native support of this feature in Julia: https://github.com/JuliaLang/julia/pull/24990.

In my opinion, I'd love to see this baked in as part of the language. Getting buy in from my team to use this in production is just not going to happen. But cool package though!

Re: More intuitive partial function application in Python

#15

I was gonna say cool for FP guys but alien to the Python world, but then i saw the f(..., x=1) syntax and decided you're a genius. Really nice idea. In practice i worry about static checkers and IDE param hints though, how have you fared with these? Not your fault at all, but mypy/pyright have very poor support for wrappers like this, and in IDEs it's common for their arg hints to devolve to a copout (args, kwargs) a…

> Not your fault at all, but mypy/pyright have very poor support for wrappers like this, and in IDEs it's common for their arg hints to devolve to a copout (args, kwargs) as well.

You might be able to annotate it accurately with generics, ParamSpec and Concatenate[1].

[1] https://www.python.org/dev/peps/pep-0612/

Re: More intuitive partial function application in Python

#16

Great to see this! Thanks for working on it. Could you compare it to functools.partial? In the example on the Readme: import better_partial as bp @bp.partial def some_operation(x, p1, p2): return (x + p1) * p2 It claims that the bp approach is superior because: func = some_operation(bp._, 10, 20) ...is better than: func = lambda x: some_operation(x, 10, 20) But is it better than just: partial(some_operation, p1=10, p…

  partial(some_operation, p1=10, p2=20)
Suffers from inconsistent syntax compared to normal function call. This is probably a negative, but it could be a positive.

Re: More intuitive partial function application in Python

#20

I'm sure it's nice, but adding another dependency to save some keystrokes in an edge case --- no thanks. If I want lots of partial application I'll use Haskell.

Also thought the benefit too small for the drag of another dependency.
Post reply on HN