Created
April 29, 2013 06:43
-
-
Save sidchilling/5480066 to your computer and use it in GitHub Desktop.
Decorator Maker - how to pass arguments to the decorators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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