A decorator is a function that takes another function and extends its behavior without explicitly modifying it. It's a powerful tool that allows you to add functionality to an existing code in a clean and readable way. #Define a 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 #Apply the decorator to a function @my_decorator def say_hello(): print("Hello!") #Call the decorated function say_hello() Explanation: 1. The my_decorator function takes another function (func) as an argument. 2. Inside my_decorator, a new function (wrapper) is defined. This function calls the original function (func) and also does some additional work before and after calling func. 3. The wrapper function is returned by my_decorator. 4. The @my_decorator syntax before the say_hello function definition applies the my_decorator decorator to say_hello. This means that when say_hello is called, it will actually call the wrapper function returned by my_decorator. 5. When say_hello() is called, it will print: Explanation of Parameters 1. decorator_name(func): decorator_name: This is the name of the decorator function. func: This parameter represents the function being decorated. When you use a decorator, the decorated function is passed to this parameter. 2. wrapper(*args, **kwargs): wrapper: This is a nested function inside the decorator. It wraps the original function, adding additional functionality. *args: This collects any positional arguments passed to the decorated function into a tuple. **kwargs: This collects any keyword arguments passed to the decorated function into a dictionary. The wrapper function allows the decorator to handle functions with any number and types of arguments. 3. @decorator_name: This syntax applies the decorator to the function_to_decorate function. It is equivalent to writing function_to_decorate = decorator_name(function_to_decorate). Something is happening before the function is called. Hello! Something is happening after the function is called. Without Decorator Function: ########################## def say_hello(): print("Hello!") #Call the decorated function say_hello() Output: Hello! Ex-2 : User Authentication: def requires_authentication(func): def wrapper(user, *args, **kwargs): if not user.get('is_authenticated', False): print("User is not authenticated. Access denied.") return None return func(user, *args, **kwargs) return wrapper # Apply the decorator @requires_authentication def view_profile(user): print(f"Viewing profile of {user['username']}") # Test the decorated function user1 = {'username': 'Alice', 'is_authenticated': True} user2 = {'username': 'Bob', 'is_authenticated': False} view_profile(user1) view_profile(user2) #Output: Viewing profile of Alice User is not authenticated. Access denied. #Without Decorator function def view_profile(user): print(f"Viewing profile of {user['username']}") # Test the decorated function user1 = {'username': 'Alice', 'is_authenticated': True} user2 = {'username': 'Bob', 'is_authenticated': False} view_profile(user1) view_profile(user2) #output Viewing profile of Alice Viewing profile of Bob
To live a creative life we must lose the fear of being wrong.
Tuesday, January 14, 2025
Decorator Function in python
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment