What are decorators?

Last updated 2 years, 11 months ago | 1119 views 75     5

Tags:- Python

Python | What are decorators

A decorator in Python is a function that modifies the behavior of an existing function or class without any modification to the original source code. 

A decorator takes another function as its argument and returns yet another function. They are represented @decorator_name in Python and are called in a bottom-up fashion.

# decorator function to convert to uppercase
def decor(func):
    def wrapper(s):
        s = s.upper()
        return(func(s))
    return inner

@decor
def check(s):
    print(s)


check('Hello Python')    # HELLO PYTHON