How to use Decorators in Python

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

Everything in Python is an object

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')

Returning functions from functions

A function can also be an argument for another function

def user_define_decorator_func(func):
def inner_wrapper_func():
# do something before executing the func
func()
# do something after executing the func
return inner_wrapper_func

How to Add Arguments to Decorators in Python

Use-cases for Python Decorators

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store