Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Phthon Decorator I just start to learn the Python, I don\'t know how those examp

ID: 3732062 • 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 means?execute all the decorator/package examples accompanied. Make useful notes for yourself including what you learned in each example.

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

Decorators in python are used to modify another part of a program at the compile time execution.

Basically, a decorator takes in a function, adds some functionality and returns it.

We can use the @ symbol along with the name of the decorator function and place it above the definition of the function to be decorated. For example,

@ make_pizza(func)                                                                                                                                                    def order_pizza():
    print("Let's order a pizza")

is equivalent to

def order_pizza():
    print("Let's order a pizza")
pizza = make_pizza(order_pizza)

Code Explanation

Decorator 1: I have modified the code slightly just to run it and explain it to you:

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
  
if __name__ == "__main__":
  
    parent_child = parent(100)
    print(parent_child())

o/p:

O/p : Printing from the second_child()

Explaination:In the above code the parent(num) is the decorator. We call the decorator by creating a parent_child() and storing the result in it , if the value passed is 10 then first function is called else second is called.

Decorator 2:

Here the my_decorator2 is called which sends just_some_function2 as a parameter. The the wrapper function calls some_function() which is just_some_function2 () ans returns the result which then returns as wrapper.The this wrapper is given to just_some_function and prints it.

Decorator 3:

Following the code and output:

In the above code , to simplify the calling of decorators using the @ symbol (this is called “pie” syntax).

Which justs removes the need to call just_some_function = my_decorator3(just_some_function3).

Decorator 4:

Code and Solution:

here the same concept is used as Decorator 3 , just the difference is that it is now calling one more function decorator.