logo

Crowdly

What is the primary purpose of the decorator below? import time def timer(func...

✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.

What is the primary purpose of the decorator below?

import time

def timer(func):

    def wrapper(*args, **kwargs):

        start_time = time.time()

        result = func(*args, **kwargs)

        end_time = time.time()

        print(f"{func.__name__} took {end_time - start_time:.2f} seconds to run.")

        return result

    return wrapper

@timer

def slow_function():

    time.sleep(2)

    print("Function execution completed.")

slow_function()
0%
0%
0%
More questions like this

Want instant access to all verified answers on eex.dev.iitm.ac.in?

Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!