Created
February 2, 2013 17:00
-
-
Save rossdylan/4698298 to your computer and use it in GitHub Desktop.
implicit function currying in python
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
""" | |
Decorator which takes a int which is the maximum number of args the decorated function can take | |
""" | |
class curryable(object): | |
def __init__(self, numArgs): | |
self.numArgs = numArgs | |
def __call__(self, func): | |
if self.numArgs > 0: | |
@curryable(self.numArgs-1) | |
def wrapper(*args, **kwargs): | |
if len(args) < self.numArgs: | |
return partial(func, *args) | |
else: | |
return partial(func, *args)(**kwargs) | |
return wrapper | |
else: | |
return func | |
@curryable(5) | |
def test_func(one,two,three,four,five): | |
print(one) | |
print(two) | |
print(three) | |
print(four) | |
print(five) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment