How to use Decorators in Python

Jack Dong
5 min readAug 21, 2022

Decorators are a significant part of python. In simple words: it is a programming technique to extend the functionality of classes or functions without modifying them.

In this tutorial, you will learn:

  • What is the decorator, and why is it useful
  • Function decorators and how to use them

Everything in Python is an object

A function is an object which means it can be assigned to a variable

We can access the function from the variable I assigned it to.

A function can be nested within another function

def hello_world(userName='Jack'):    print('1.you are inside the hello_world() function')    def hi():        return "you are in the hi() function"    def welcome():        return "you are in the welcome() function"    print(hi())    print(welcome())    print('2. you are back to the hello_world() function')

--

--

Responses (1)