Skip to content

Instantly share code, notes, and snippets.

@graingert
Created Jun 10, 2014
Embed
What would you like to do?
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