Skip to content

Instantly share code, notes, and snippets.

@pohmelie
Created November 9, 2018 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pohmelie/1fc8243518126dd41bc35656ef2d9777 to your computer and use it in GitHub Desktop.
Save pohmelie/1fc8243518126dd41bc35656ef2d9777 to your computer and use it in GitHub Desktop.
Curry weird decorator
import functools
def curry(calls=1):
def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
class Curry:
def __init__(self, *args, **kwargs):
self.args = list(args)
self.kwargs = kwargs
self.calls = calls
def __call__(self, *args, **kwargs):
self.args.extend(args)
self.kwargs.update(kwargs)
if self.calls > 1:
self.calls -= 1
return self
return f(*self.args, **self.kwargs)
return Curry()(*args, **kwargs)
return wrapper
return decorator
@curry(2)
def add(x, y):
print(x, y)
return x + y
print(add)
print(add(1))
print(add(1)(2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment