Skip to content

Instantly share code, notes, and snippets.

@ReitenSchnell
Created May 24, 2012 20:58
Show Gist options
  • Save ReitenSchnell/2784212 to your computer and use it in GitHub Desktop.
Save ReitenSchnell/2784212 to your computer and use it in GitHub Desktop.
Decorators
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