Created
May 24, 2012 20:58
-
-
Save ReitenSchnell/2784212 to your computer and use it in GitHub Desktop.
Decorators
This file contains hidden or 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
from functools import update_wrapper | |
def decorator(d): | |
return lambda fn: update_wrapper(d(fn), fn) | |
decorator = decorator(decorator) | |
@decorator | |
def n_ary(f): | |
"""Given binary function f(x, y), return an n_ary function such | |
that f(x, y, z) = f(x, f(y,z)), etc. Also allow f(x) = x.""" | |
def n_ary_f(x, *args): | |
return x if not args else f(x, n_ary_f(*args)) | |
return n_ary_f | |
@n_ary | |
def add(x,y): | |
return x + y | |
print add(2, 3, 4, 5, 6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment