Created
June 10, 2014 08:13
-
-
Save graingert/1bb3a54dbee084b000a1 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
from functools import wraps | |
class MyDecorator(object): | |
""" | |
would normally be my_decorator because it's a function, and functions use underscore delimited lower-case. | |
Used CamelCase to demo that it's really a class. | |
""" | |
def __init__(multiplier): | |
self.multiplier = multiplier | |
def __call__(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
return self.multiplier * func(*args, **kwargs) | |
return wrapper | |
@MyDecorator(3): | |
def foo(): | |
return 4 | |
concrete_decorator=MyDecorator(4) | |
@concrete_decorator | |
def bar(): | |
return 5 | |
assert(foo() == 12) | |
assert(bar() == 20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment