Phthon Decorator I just start to learn the Python, I don\'t know how those examp
ID: 3733017 • Letter: P
Question
Phthon Decorator
I just start to learn the Python, I don't know how those example below work. Can anyone tell me what each example output and meaning , help me to better understand the decorator?Thank you very much. Execute all the decorator/package examples accompanied. Make useful notes for yourself including what you learned in each example. Thank you very much.
import unittest
# 1. Decorator1
# idea 1
# python can return a method
# idea 2
# python can nest methods
def parent(num):
def first_child():
return "Printing from the first_child() function."
def second_child():
return "Printing from the second_child() function."
# Python Style: Don't afraid of using exceptions
if num == 10:
return first_child
else:
return second_child
# 2. Decorator 2
# decorator is a pattern to
# 1. get a function
# 2. returns wrapper (inner function) that wraps the inner function.
def my_decorator2(some_function):
def wrapper():
result = some_function()
return result
return wrapper
def just_some_function2():
print("Wheee!")
# 3. Decorator 3
# syntactic sugar
def my_decorator3(some_function):
def wrapper(num):
if num == 10:
value = "Yes!"
else:
value = "No!"
result = some_function()
return value + ":" + result
return wrapper
@my_decorator3
def just_some_function3():
return "Wheee!"
just_some_function3(10)
# 4. Decorator 4
# syntactic sugar
def outside_decorator(expr):
def my_decorator4(some_function):
def wrapper(num):
if num == 10:
value = "Yes!"
else:
value = "No!"
result = some_function()
return expr + ":" + value + ":" + result
return wrapper
return my_decorator4
@outside_decorator("Python decorators")
def just_some_function4():
return "Wheee!"
class DecoratorTests(unittest.TestCase):
def test_decorator1(self):
foo = parent(10)
bar = parent(11)
self.assertEqual(foo(), "Printing from the first_child() function.")
self.assertEqual(bar(), "Printing from the second_child() function.")
def test_decorator2(self):
def just_some_function():
return "Wheee!"
expected_string = "Wheee!"
just_some_function = my_decorator2(just_some_function)
self.assertEqual(just_some_function(), expected_string)
def test_decorator3(self):
def just_some_function():
return "Wheee!"
expected_string = "Yes!:" + "Wheee!"
self.assertEqual(just_some_function3(10), expected_string)
def test_decorator4(self):
@outside_decorator("Python decorators")
def just_some_function4():
return "Wheee!"
expected_string = "Python decorators:"
"Yes!:" + "Wheee!"
self.assertEqual(just_some_function4(10), expected_string)
if __name__ == '__main__':
unittest.main()
Explanation / Answer
import unittest #unit testing framework
# I made notes of each and everydecorator separately for better understanding
# Decorator 1
The output of the first two print statements is:
This simply means that foo points to the first_child() function, while bar points to the second_child() function.
The output of the second two functions confirms this:
# Decorator 2
Output will be:
Something is happening before some_function() is called.
# Decorator 3
output will be:
Yes!
#Decorator 4
Python allows you to simplify the calling of decorators using the @ symbol (this is called “pie” syntax).
def outside_decorator(expr): #definition of a decorator
def my_decorator4(some_function): #decorator within a decorator
def wrapper(num): # wrapper within decorator
if num == 10:
value = "Yes!"
else:
value = "No!"
result = some_function()
return expr + ":" + value + ":" + result
return wrapper
return my_decorator4
@outside_decorator("Python decorators") #passing our function to decorator
def just_some_function4(): #defining our function
return "Wheee!"
class DecoratorTests(unittest.TestCase): # used to test code and gives the results of ran tests and time
def test_decorator1(self): #it is used here to test all the decorators 1,2,3,4 in a single example
foo = parent(10)
bar = parent(11)
self.assertEqual(foo(), "Printing from the first_child() function.")
self.assertEqual(bar(), "Printing from the second_child() function.")
def test_decorator2(self):
def just_some_function():
return "Wheee!"
expected_string = "Wheee!"
just_some_function = my_decorator2(just_some_function)
self.assertEqual(just_some_function(), expected_string)
def test_decorator3(self):
def just_some_function():
return "Wheee!"
expected_string = "Yes!:" + "Wheee!"
self.assertEqual(just_some_function3(10), expected_string)
def test_decorator4(self):
@outside_decorator("Python decorators")
def just_some_function4():
return "Wheee!"
expected_string = "Python decorators:"
"Yes!:" + "Wheee!"
self.assertEqual(just_some_function4(10), expected_string)
if __name__ == '__main__':
unittest.main()