Assignment versus binding

Chris Angelico rosuav at gmail.com
Wed Oct 5 09:44:58 EDT 2016


On Thu, Oct 6, 2016 at 12:13 AM, Gregory Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> Chris Angelico wrote:
>>
>> How do you handle variadic functions?
>
>
> All functions in Haskell take exactly one argument, so
> there isn't really any such thing as a variadic function.
> The argument can be a list, however, so you can get the
> same effect.

That basically just means you always pack and unpack *args. If you're
talking about currying a function - particularly *automatic* currying
- functions have to take multiple arguments, and in a naive
implementation, a fixed number. Something like:

def auto_curry(f):
    @functools.wraps(f)
    def curry_or_run(*args):
        if len(args) >= f.__code__.co_argcount:
             return f(*args)
        return functools.partial(curry_or_run, *args)
    return curry_or_run

@auto_curry
def f(a, b, c):
    return a + b * c

print(f(1)(2, 3))
print(f(1, 2)(3))
print(f(1, 2, 3))
print(f(1)(2)(3))

That works fine as long as you can probe the function for co_argcount.
(And as long as you don't have keyword arguments, but they're a bit of
a Python peculiarity so I doubt that'll get in the way.) Hence my
query about how variadic functions and automatic currying work - how
does it know whether to curry or run?

ChrisA



More information about the Python-list mailing list