Skip to content

Instantly share code, notes, and snippets.

@sidchilling
Created April 29, 2013 06:42
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 sidchilling/5480054 to your computer and use it in GitHub Desktop.
Save sidchilling/5480054 to your computer and use it in GitHub Desktop.
Script to show how to make a generic decorator which will take all the arguments passed to the decorated function
# To make a generic decorator use *args and **kwargs
def a_decorator_passing_arbitrary_arguments(function_to_decorate):
# The wrapper accepts any arguments
def a_wrapper_accepting_arbitrary_arguments(*args, **kwargs):
print 'Do I have args?'
print args
print kwargs
function_to_decorate(*args, **kwargs)
return a_wrapper_accepting_arbitrary_arguments
@a_decorator_passing_arbitrary_arguments
def function_with_no_arguments():
print 'Python is cool, no arguments here'
function_with_no_arguments()
@a_decorator_passing_arbitrary_arguments
def function_with_arguments(a, b, c):
print a, b, c
function_with_arguments('Harry', 'Ron', 'Hermione')
@a_decorator_passing_arbitrary_arguments
def function_with_named_arguments(a, b, c, want = 'Invisibilty Cloak'):
print '%s, %s and %s want the %s' %(a, b, c, want)
function_with_named_arguments('Harry', 'Ron', 'Hermione', want = 'Elder Wand')
class Ginny(object):
def __init__(self):
self.age = 31
@a_decorator_passing_arbitrary_arguments
def say_your_age(self, lie):
print 'I am %s years old' %(self.age - lie)
g = Ginny()
g.say_your_age(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment