DEV Community

Srinivasulu Paranduru
Srinivasulu Paranduru

Posted on

Decorators in Python

Decorators are flexible way to modify or extend behavior of functions or methods, without changing their actual code.

  • A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality.
  • Decorators are often used in scenarios such as logging, authentication and memorization, allowing us to add additional functionality to existing functions or methods in a clean, reusable way.

Now you can easily add on extra functionality with a decorator

  @some_decorator
  def simple_func():
    # Do Simple stuff
    return something
Enter fullscreen mode Exit fullscreen mode
  • We will go through the steps of manually building out a decorator ourselves, to show what @operator is doing behind the scenes
def func():
 return 1

print(func())

print(func)
Enter fullscreen mode Exit fullscreen mode

Output
1

def hello():
  return "Hello!"

print(hello())  # Hello! 

print(hello) #  <function hello at 0x000001CC0184FA00>

#Copy one function to another

greet = hello

print(greet()) #'Hello!'

print(hello())
# o/p: 'Hello!'


# Deleting function
del hello

print(hello())
=> It will throw error as function hello deleted already

print(greet())
# o/p: 'Hello!'

Enter fullscreen mode Exit fullscreen mode

Examples:1

def hello(name='Raju')
 print('The hello() function has been executed!')

 def greet():
   return '\t This is the greet() func inside hello!'

 def welcome():
    return '\t This is welcome() inside hello'

 print(greet())
 print(welcome())
 print('This is the end of the hello function!')
Enter fullscreen mode Exit fullscreen mode

Examples:2

def hello(name='Raju')
 print('The hello() function has been executed!')

 def greet():
   return '\t This is the greet() func inside hello!'

 def welcome():
    return '\t This is welcome() inside hello'

 if name == 'Raju':
   return greet
 else :
   return welcome


my_new_func = hello('Raju')
#o/P:

print(my_new_func())
Enter fullscreen mode Exit fullscreen mode

Example:3

def cool():
   def super_cool():
      return 'I am very cool!'

   return super_cool

some_func=cool()
some_func
some_func()

Enter fullscreen mode Exit fullscreen mode

Passing function as a parameter to function

Example:1

def hello():
  return 'Hi Jose!'

def other(some_def_func):
  print('Other code runs here!')
  print(some_def_func())

# passing raw function not executable function as parameter
other(Hello)
Enter fullscreen mode Exit fullscreen mode

Output : It will throw error

Example:2

def new_decorator(orginal_func):
  def wrap_func():
     print('Some extra code, before the original function')
     original_func()
     print('Some extra code, after the original function!')

  return wrap_func

def func_needs_decorator():
  print("I want to be decorated!!")

func_needs_decorator()
o/p:

decorated_func = new_decorator(func_needs_decorator)

decorated_func()


# decorator
@new_decorator
def func_needs_decorator():
  print("I want to be decorated!!")

# check the output

def func_needs_decorator():
  print("I want to be decorated!!")

# check the output
Enter fullscreen mode Exit fullscreen mode

References to extend your decorators concepts
1.Decorators are used for web frameworks

2.View Decorators

3.Creating web pages in python using Django

Top comments (0)