Decorators¶
1. Function as Variable¶
Assign functions to variables
def greet(name):
return "Hello "+name
greet_someone = greet
print(greet_someone("Saurabh"))
# Outputs: Hello Saurabh
2. Nested Function Definition¶
Define functions inside other functions
def greet(name):
def get_message():
return "Hello "
result = get_message()+name
return result
print(greet("Saurabh"))
# Outputs: Hello Saurabh
3. Function as Parameter¶
Functions can be passed as parameters to other functions
def greet(name):
return "Hello " + name
def call_func(func):
other_name = "Saurabh"
return func(other_name)
print(call_func(greet))
# Outputs: Hello Saurabh
4. Nested Return Functions¶
Functions can return other functions
def compose_greet_func():
def get_message():
return "Hello there!"
return get_message
greet = compose_greet_func()
print(greet())
# Outputs: Hello there!
5. Scope of Inner Functions¶
Inner functions have access to the enclosing scope
def compose_greet_func(name):
def get_message():
return "Hello there "+name+"!"
return get_message
greet = compose_greet_func("Saurabh")
print(greet())
# Outputs: Hello there Saurabh!
6. Composition of Decorators¶
Composition of Decorators
def get_text(name):
return "lorem ipsum, {0} dolor sit amet".format(name)
def p_decorate(func):
def func_wrapper(name):
return "<p>{0}</p>".format(func(name))
return func_wrapper
my_get_text = p_decorate(get_text)
print(my_get_text("Saurabh"))
# <p>Outputs lorem ipsum, Saurabh dolor sit amet</p>
7. Anotation Formation¶
Decorate Itself
def get_text(name):
return "lorem ipsum, {0} dolor sit amet".format(name)
def p_decorate(func):
def func_wrapper(name):
return "<p>{0}</p>".format(func(name))
return func_wrapper
get_text = p_decorate(get_text)
print(get_text("Saurabh"))
# <p>Outputs lorem ipsum, Saurabh dolor sit amet</p>
8. Decorator Syntax¶
Python’s Decorator Syntax
def p_decorate(func):
def func_wrapper(name):
return "<p>{0}</p>".format(func(name))
return func_wrapper
@p_decorate
def get_text(name):
return "lorem ipsum, {0} dolor sit amet".format(name)
print(get_text("Saurabh"))
# Outputs <p>lorem ipsum, Saurabh dolor sit amet</p>
9. Multiple Decorate¶
Multiple Decorate functions
def get_text(name):
return "lorem ipsum, {0} dolor sit amet".format(name)
def p_decorate(func):
def func_wrapper(name):
return "<p>{0}</p>".format(func(name))
return func_wrapper
def strong_decorate(func):
def func_wrapper(name):
return "<strong>{0}</strong>".format(func(name))
return func_wrapper
def div_decorate(func):
def func_wrapper(name):
return "<div>{0}</div>".format(func(name))
return func_wrapper
get_text = div_decorate(p_decorate(strong_decorate(get_text)))
print(get_text("Saurabh"))
# Outputs <div><p><strong>lorem ipsum, Saurabh dolor sit amet</strong></p></div>
10. Decorator Flavour¶
Decorator Flavour
def get_text(name):
return "lorem ipsum, {0} dolor sit amet".format(name)
def p_decorate(func):
def func_wrapper(name):
return "<p>{0}</p>".format(func(name))
return func_wrapper
def strong_decorate(func):
def func_wrapper(name):
return "<strong>{0}</strong>".format(func(name))
return func_wrapper
def div_decorate(func):
def func_wrapper(name):
return "<div>{0}</div>".format(func(name))
return func_wrapper
@div_decorate
@p_decorate
@strong_decorate
def get_text(name):
return "lorem ipsum, {0} dolor sit amet".format(name)
print(get_text("Saurabh"))
# Outputs <div><p><strong>lorem ipsum, Saurabh dolor sit amet</strong></p></div>