Skip to content

Instantly share code, notes, and snippets.

@johnpena
Created November 17, 2015 00:51
Show Gist options
  • Save johnpena/d4d9049edb7bd130a6e6 to your computer and use it in GitHub Desktop.
Save johnpena/d4d9049edb7bd130a6e6 to your computer and use it in GitHub Desktop.
My preferred way of making a decorator with arguments
class great_stuff(object):
def __init__(self, a, b, c):
"""
__init__ is *not* passed the function to be decorated if arguments
are passed to the decorator.
"""
self.a = a
self.b = b
self.c = c
def __call__(self, func):
"""
FYI, this is only called once as part of the decoration process.
It is not re-called on each function call. It can only have one
argument, the function being decorated.
"""
print "we're inside __call__"
def wrapper(*args):
print "we're inside the wrapper."
print "our arguments are {a}, {b} and {c}".format(a=self.a, b=self.b, c=self.c)
func(*args)
print "func was called"
return wrapper
@great_stuff("a", "b", "c")
def boop(x, y, z, w):
print "now we're in boop with {x}, {y}, {z} and {w}".format(x=x, y=y, z=z, w=w)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment