0

I understand if we don't return anything, we end up with nothing with return variable. But when I look at decorator example:

import time


def timing_function(some_function):

    """
    Outputs the time a function takes
    to execute.
    """

    def wrapper():
        t1 = time.time()
        some_function()
        t2 = time.time()
        return "Time it took to run the function: " + str((t2 - t1)) + "\n"
    return wrapper

for the first return, are we return "Time it took to run the function: " + str((t2 - t1)) + "\n" this value to wrapper function then at second return, we return wrapper value, which is "Time it took to run the function: " + str((t2 - t1)) + "\n" again to timing_function?

Then how about this code:

def my_decorator(some_function):

 def wrapper():

     num = 10

     if num == 10:
         print("Yes!")
     else:
         print("No!")

     some_function()

     print("Something is happening after some_function() is called.")

 return wrapper

What and why are we returning to wrapper()? or there is nothing returning to wrapper() but we return wrapper to my_decorator() which is nothing?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
FunctionBlock
  • 13
  • 1
  • 6

0 Answers0