| TOP | Code | Math | Luke | Math Book |
Previous: Why numbering should start at zero Next: Yet Another e Explained

One-liner curry decorator in Python

2019-03-01

I discovered the following trick.

from functools import partial
curry = lambda n: partial(*[partial] * n)

Usage:

@curry(3)
def add(x, y, z):
    return x + y + z

print(add(2)(3)(4))
# output = 9

A more obfuscated way will be:

from functools import partial
curry = lambda f: partial(*[partial] * f.__code__.co_argcount)(f)

Usage

@curry
def add(x, y, z):
    return x + y + z

print(add(2)(3)(4))
# output = 9
Feedbacks are welome: @czheo