You definitely don't want to do this with exec
.
You can find recipes for partial
in pure Python, such as this one—many of them are mislabeled as curry
recipes, so look for that as well. At any rate, these will show you the proper way to do it without exec
, and you can just pick one and modify it to do what you want.
Or you could just wrap partial
…
However, whatever you do, there's no way the wrapper can know that it's defining a function named "five"; that's just the name of the variable you store the function in. So if you want a custom name, you'll have to pass it in to the function:
five = my_partial('five', len, 'hello')
At that point, you have to wonder why this is any better than just defining a new function.
However, I don't think this is what you actually want anyway. Your ultimate goal is to define a @curry
decorator that creates a curried version of the decorated function, with the same name (and docstring, arg list, etc.) as the decorated function. The whole idea of replacing the name of the intermediate partial
is a red herring; use functools.wraps
properly inside your curry
function, and it won't matter how you define the curried function, it'll preserve the name of the original.
In some cases, functools.wraps
doesn't work. And in fact, this may be one of those times—you need to modify the arg list, for example, so curry(len)
can take either 0 or 1 parameter instead of requiring 1 parameter, right? See update_wrapper
, and the (very simple) source code for wraps
and update_wrapper
to see how the basics work, and build from there.
Expanding on the previous: To curry a function, you pretty much have to return something that takes (*args)
or (*args, **kw)
and parse the args explicitly, and possibly raise TypeError
and other appropriate exceptions explicitly. Why? Well, if foo
takes 3 params, curry(foo)
takes 0, 1, 2, or 3 params, and if given 0-2 params it returns a function that takes 0 through n-1 params.
The reason you might want **kw
is that it allows callers to specify params by name—although then it gets much more complicated to check when you're done accumulating arguments, and arguably this is an odd thing to do with currying—it may be better to first bind the named params with partial
, then curry
the result and pass in all remaining params in curried style…
If foo
has default-value or keyword args, it gets even more complicated, but even without those problems, you already need to deal with this problem.
For example, let's say you implement curry
as a class that holds the function and all already-curried parameters as instance members. Then you'll have something like this:
def __call__(self, *args):
if len(args) + len(self.curried_args) > self.fn.func_code.co_argcount:
raise TypeError('%s() takes exactly %d arguments (%d given)' %
(self.fn.func_name, self.fn.func_code.co_argcount,
len(args) + len(self.curried_args)))
self.curried_args += args
if len(self.curried_args) == self.fn.func_code.co_argcount:
return self.fn(*self.curried_args)
else:
return self
This is horribly oversimplified, but it shows how to handle the basics.