60 karma · joined September 15, 2017
@partial
def f3(x, y, z):
return (x, y, z)
@partial
def f2(x, y):
return (x, y)
A curried version of f2 would look like: curried_f2 = lambda x: lambda y: f2(x, y)
curried_f2(1)(2) == (1, 2)
Currying produces a chain of functions which each take one argument. So its true for f2 that currying and the partial decorator are similar: curried_f2(1) == f2(1, _) == f2(..., x=1)
as you pointed out.But now consider the 3 argument case:
curried_f3 = lambda x: lambda y: lambda z: f3(x, y, z)
curried_f3(1) != f3(1, _, _)
this is because f(1, _, _) takes two arguments whereas curried_f3(1) takes one.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, p3, p4, p5, p6, p7, p8, p9, p10):
return "stuff"
g(f(..., p4=1, p7=2))
# seems nicer than
g(lambda p1, p2, p3, p5, p6, p8, p9, p10: f(p1, p2, p3, 1, p5, p6, 2, p8, p9, p10))https://gist.github.com/chrisgrimm/64bca66f14528cfda6d865cc2...
Accordingly there are a lot of things I have to do to make better_partial a strong tool for the community. I'll probably have time to work on the project again later in the week.
https://gist.github.com/chrisgrimm/64bca66f14528cfda6d865cc2...
Essentially functools.partial is somewhat limited in terms of which arguments can be partially applied.
Coconut looks cool btw! I'll give it a closer look soon.
In my perfect world, python functions would default to behaving as they do when decorated with better_partial.partial, but it seems like a long-shot to try to get something like this added to the language itself.
Also note that the _ placeholder isn't the only way that better_partial helps you. You can also use ... to indicate that you want to omit all arguments except ones you explicitly pass as kwargs. For instance:
from better_partial import partial, _
@partial
def f(a,b,c,d,e):
return (a,b,c,d,e)
f(_,_,3,_,_)(1,2,4,5) == f(..., c=3)(1,2,4,5) from lib import external
from better_partial import partial as pp, _
pp(external)(..., arg4=10)https://gist.github.com/chrisgrimm/64bca66f14528cfda6d865cc2...