Skip to main content

Posts

Showing posts from May, 2021

Decorators in Python

Decorators In simple words, Decorator is a function that calls another function.  Decorators are very useful concept in Python. Decorators allow the programmers to enhance the functionality of a function by wrapping it with a decorator.  It is similar to having a function with in a function. Before we see more about decorators let's have a quick look at how it is like to wrap a function within another function and how a function can be assigned like an object. These will help in understanding the decorators better.  Assigning function as an Object Like creating instance of a class (object), the same can be done with functions as well.  We can define a function and create instances of a function. Function can either be called directly or by using the instance created.  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # Define a function to print "Hello World" def hello_world ():     print ( "Hello World, This is a original function" ) # Call the function directly hello_world(