Question

Using Python 3 Write a decorator named debug that prints "Entering " followed by the name...

Using Python 3 Write a decorator named debug that prints "Entering " followed by the name of the decorated function (func.__name__) before calling it and prints "Exiting " followed by the name of the decorated function after calling it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python code is given below:

def debug(func):
    fname = func.__name__   # getting the file name
    def inner(*args, **kwargs):
        print("Entering " + fname)   # printing result before calling of actual function
        func(*args, **kwargs)        # calling of actual function
        print("Exiting " + fname)   # printing result after calling of actual function
    return inner

# main function 
@debug
def main():
        print("Main")

if __name__ == '__main__':
    main()

Sample Output:
Entering main
Main
Exiting main

Screenshot of the code is given below:

If the answer helped please upvote, it means a lot and for any query please comment.

Add a comment
Know the answer?
Add Answer to:
Using Python 3 Write a decorator named debug that prints "Entering " followed by the name...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT