Created
November 9, 2018 16:32
-
-
Save pohmelie/1fc8243518126dd41bc35656ef2d9777 to your computer and use it in GitHub Desktop.
Curry weird decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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