Y
HN Search
Hacker News Search
new
|
comments
|
top
|
jobs
cgrimm1994
searching Neon…
1.
▲
2.
▲
3.
▲
4.
▲
5.
▲
6.
▲
7 ms
·
1.
▲
by
cgrimm1994
2y ago
Excited to be out of stealth!
2.
▲
by
cgrimm1994
5y ago
I think the similarity ends when you consider functions with more than 2 arguments. Let's compare a 2 argument function with a 3 argument function: @partial def f3(x, y, z): return (x, y, z) @partial def f2(x, y):
3.
▲
by
cgrimm1994
5y ago
No worries!
4.
▲
by
cgrimm1994
5y ago
I'm glad you like the recipe :-) What if you have a function "f" that takes like 10 arguments and you need to pass it to a function "g" that expects as input a function takes 8 arguments. @partial def f(p1, p2
5.
▲
by
cgrimm1994
5y ago
Yes! I definitely need to beef up the README. I wrote up a gist for some other commenters below which I can incorporate as part of the justification. https://gist.github.com/chrisgrimm/64bca66f14528cfda6d865cc2...
6.
▲
by
cgrimm1994
5y ago
I'll give this a look.
7.
▲
by
cgrimm1994
5y ago
High praise :-) I'm glad you like it! I don't know much about static checks / param hinting. TBH, I coded this up because I wanted to imagine how functions should work in Python as more of a fun exercise. Accordingly there
8.
▲
by
cgrimm1994
5y ago
I'm glad you like it! This project was basically me trying to visualize how I wish functions generally worked like in Python.
9.
▲
by
cgrimm1994
5y ago
I posted this below, but I can put it here too: https://gist.github.com/chrisgrimm/64bca66f14528cfda6d865cc2... Essentially functools.partial is somewhat limited in terms of which arguments can be partially applied.
10.
▲
by
cgrimm1994
5y ago
Sure, I'm not claiming to have originated the idea of using _ or some other placeholder to return partially applied functions. Lots of languages have some kind of shorthand syntax for this. I just hadn't seen it done in Python bef
11.
▲
by
cgrimm1994
5y ago
:-)
12.
▲
by
cgrimm1994
5y ago
Thanks for the feedback! This project was really me just imagining how I'd like functions to behave in Python. I didn't really expect there to be this much interest.
13.
▲
by
cgrimm1994
5y ago
Yes! I almost made this mistake. Although I'm not sure @partial is the most apt name for the decorator, as it produces a function which can be partially applied. In retrospect, @partialize or something like it might be more appropriate
14.
▲
by
cgrimm1994
5y ago
I'll look into this soon. I honestly wasn't expecting this much traffic on what essentially was a weekend project, so I'll need to spend a bit of time making the code less insane and testing for things like pickle-ability.
15.
▲
by
cgrimm1994
5y ago
I'm glad you like it! I agree it's not optimal to have to wrap existing functions. One option would be to decorate the functions as they're defined, although you cant do this for external functions. In my perfect world, pytho
16.
▲
by
cgrimm1994
5y ago
I guess it depends what kind of stuff you're working on. I do a lot of work with jax for my research ( https://github.com/google/jax ) and use functools.partial a lot.
17.
▲
by
cgrimm1994
5y ago
Thanks! I would love to see it baked in as well. It'd be nice if, (1) all functions defaulted to behaving like better_partial.partial decorated functions and (2) similar to Ellipsis, there was some other special sentinel that we could
18.
▲
by
cgrimm1994
5y ago
As pointed out, you could import _ directly into your namespace, or map it to some other shorthand name that doesn't interfere with your naming conventions. Also note that the _ placeholder isn't the only way that better_partial h
19.
▲
by
cgrimm1994
5y ago
One option would be to wrap the functions from lib import external from better_partial import partial as pp, _ pp(external)(..., arg4=10)
20.
▲
by
cgrimm1994
5y ago
I really appreciate that :) Definitely give it a spin and let me know what you think!
21.
▲
by
cgrimm1994
5y ago
here's an example of one of the behaviors of functools.partial that I find unintuitive / undesirable: https://gist.github.com/chrisgrimm/64bca66f14528cfda6d865cc2...
22.
▲
by
cgrimm1994
5y ago
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 wa
23.
▲
More intuitive partial function application in Python
(github.com)
102 points
by
cgrimm1994
5y ago
|
49 comments