Skip to content

Instantly share code, notes, and snippets.

@mebusw
Last active August 29, 2015 14:05
Show Gist options
  • Save mebusw/c0b757e6fbeda0065eb5 to your computer and use it in GitHub Desktop.
Save mebusw/c0b757e6fbeda0065eb5 to your computer and use it in GitHub Desktop.
class decoratorWithArguments(object):
def __init__(self, arg1, arg2, arg3):
"""
If there are decorator arguments, the function
to be decorated is not passed to the constructor!
"""
print "Inside __init__()"
self.arg1 = arg1
self.arg2 = arg2
self.arg3 = arg3
def __call__(self, f):
"""
If there are decorator arguments, __call__() is only called
once, as part of the decoration process! You can only give
it a single argument, which is the function object.
"""
print "Inside __call__()"
def wrapped_f(*args):
print "Inside wrapped_f()"
print "Decorator arguments:", self.arg1, self.arg2, self.arg3
f(*args)
print "After f(*args)"
return wrapped_f
@decoratorWithArguments("hello", "world", 42)
def sayHello(a1, a2, a3, a4):
print 'sayHello arguments:', a1, a2, a3, a4
##############################
sayHello("say", "hello", "argument", "list")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment