Created
August 7, 2009 20:02
-
-
Save inklesspen/164133 to your computer and use it in GitHub Desktop.
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 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