Skip to content

Instantly share code, notes, and snippets.

@sidchilling
Created April 29, 2013 06:43
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/5480066 to your computer and use it in GitHub Desktop.
Save sidchilling/5480066 to your computer and use it in GitHub Desktop.
Decorator Maker - how to pass arguments to the decorators
# This script will show how a decorator maker works. This is required for understanding how
# we can pass arguments to the decorators
def decorator_maker():
print 'I make decorators. I get executed only once when you make or create a decorator'
def my_decorator(func):
print 'I am a decorator. I get executed only one when you decorate a function'
def wrapper():
print 'I am the wrapper. I am executed everytime you call the decorated function'
return func()
print 'As the decorator, I return the wrapper function'
return wrapper
print 'As the decorator maker, I return the decorator'
return my_decorator
# Now we will decorate a function using the decorator maker. We will have to call the decorator
# maker which will return the decorator using which we will decorate the function
@decorator_maker()
def decorated_function():
print 'I am the decorated function'
# Now we will call the decorated function twice
decorated_function()
decorated_function()
# As you saw we used the @ syntax with a function call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment