Decorators in Python

Decorators are simply functions in python that add extra functionality to the current function without changing the structure of the current function.Suppose we have function that we already written to get user name and print it.But later the requirement changes the display should be always in small letter in that case we can use Decorators in Python.Decorators in Python can be used to fulfill this by writing a Decorators functions to accomplish this task.

Decorators in Python example

def display_msg():
   return 'Welcome Back '

display_msg()  

OutPut : Welcome Back 



def decorator_lowercase(function):
   def wrapper():
       func = function()
       string_lowercase = func.lower()
       return string_lowercase
   return wrapper

@decorator_lowercase
def display_msg():
   return 'Welcome Back '

display_msg()  


OutPut : welcome back 

My Thought

Your email address will not be published. Required fields are marked *

Our Tool : hike percentage calculator