Live data from Hacker News

More intuitive partial function application in Python

github.com

1–10 of 36 posts

Re: More intuitive partial function application in Python

#2
Hey, I use functools.partial a lot in my projects and I've always felt like it was pretty limited in the types of partial function application it could accomplish. I put together a modified version of partial over the weekend that I wanted to share / get feedback on. Let me know what you all think!

Re: More intuitive partial function application in Python

#3
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) as well.

Re: More intuitive partial function application in Python

#4

Hey, I use functools.partial a lot in my projects and I've always felt like it was pretty limited in the types of partial function application it could accomplish. I put together a modified version of partial over the weekend that I wanted to share / get feedback on. Let me know what you all think!

`functools.partial` has been bothering me for a long time and I've always thought it'd be nice to have something like the placeholder `·` that people use in math (i.e. `f(·, y, z)`). It didn't occur to me that a decorator might actually get us there without having to introduce new syntax. Nice project!

Re: More intuitive partial function application in Python

#8
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, p2=20)

?

Re: More intuitive partial function application in Python

#10

Hey, I use functools.partial a lot in my projects and I've always felt like it was pretty limited in the types of partial function application it could accomplish. I put together a modified version of partial over the weekend that I wanted to share / get feedback on. Let me know what you all think!

`functools.partial` has been bothering me for a long time and I've always thought it'd be nice to have something like the placeholder `·` that people use in math (i.e. `f(·, y, z)`). It didn't occur to me that a decorator might actually get us there without having to introduce new syntax. Nice project!

What specifically bothers you about functools partial? I think the decorator is nice in certain ways. However, then you have to decorate your code and guess where you’re going toy need the partials or create wrappers just to add the decorator which seems undesirable for a number of cases. I agree that partial has warts when it comes to inspection but at least they’re easy to make anywhere you want without having to apply any changes to the callee. Nice to have another option for partials though so that when you need these features there’s a way to get them.
Post reply on HN