Skip to content

Instantly share code, notes, and snippets.

@graingert
Created June 10, 2014 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graingert/1bb3a54dbee084b000a1 to your computer and use it in GitHub Desktop.
Save graingert/1bb3a54dbee084b000a1 to your computer and use it in GitHub Desktop.
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