The use of a decorator in Python. A decorator is a function that wraps another function to add functionality to it. In this code, you have defined a decorator function my_decorator, which wraps the msg function. Here's a breakdown of the code:
The msg function is decorated with my_decorator, so when you call msg(), it prints "Something is happening before the function is called," then "Hello world!", and finally "Something is happening after the function is called." This demonstrates how decorators can add behavior to functions without modifying their source code.
# Decorator function def my_decorator(func): def wrapper(): print("Something is happening before the function is called") func() print("Something is happening after the function is called") return wrapper # Function to be decorated @my_decorator def msg(): print("Hello world !") # Call the decorated function msg()
Something is happening before the function is called Hello world ! Something is happening after the function is called
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions