Skip to content

Instantly share code, notes, and snippets.

@inklesspen
Created August 7, 2009 20:02
Show Gist options
  • Save inklesspen/164133 to your computer and use it in GitHub Desktop.
Save inklesspen/164133 to your computer and use it in GitHub Desktop.
import decorator
def multiplier(n):
# will multiply the first incoming argument by n.
def internal(fn, arg):
return fn(arg*n)
return decorator.decorator(internal)
@multiplier(3)
def foo(arg):
print arg
foo(4)
def multiplier_for_method(n):
# will multiply the first incoming argument by n.
def internal(fn, self, arg):
return fn(self, arg*n)
return decorator.decorator(internal)
class bar(object):
@multiplier_for_method(4)
def foo(self, arg):
print self
print arg
@multiplier(5) # this won't work, since baz takes two arguments, but the decorator does not.
def baz(self, arg):
print self
print arg
bar().foo(4)
bar().baz(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment