Saturday, February 22, 2020

Python Decorators : Simple Python program with decorators

Decorator functions help to add new functionality to existing function without modifying the structure of existing one.
# Defining decorator function welcome_to_school

def welcome_to_school(func):

    def welcome_message_school(*args):

        print("\n##################### Welcome to School #####################")

        func(*args) 

    return welcome_message_school;

    

@welcome_to_school

def welcome_message_chemistry(name):

    print("Welcome to Chemistry class," , name)


@welcome_to_school

def welcome_message_biology(name):

    print("Welcome to Biology class," , name)

    

welcome_message_chemistry("Chemistry Boy")

welcome_message_biology("Biology Girl")


Output:

No comments:

Post a Comment