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 functional…


This content originally appeared on DEV Community and was authored by Srinivasulu Paranduru

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

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

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

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

Example:3

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

   return super_cool

some_func=cool()
some_func
some_func()

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)

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

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

2.View Decorators

3.Creating web pages in python using Django


This content originally appeared on DEV Community and was authored by Srinivasulu Paranduru


Print Share Comment Cite Upload Translate Updates
APA

Srinivasulu Paranduru | Sciencx (2025-10-17T09:09:23+00:00) Decorators in Python. Retrieved from https://www.scien.cx/2025/10/17/decorators-in-python-2/

MLA
" » Decorators in Python." Srinivasulu Paranduru | Sciencx - Friday October 17, 2025, https://www.scien.cx/2025/10/17/decorators-in-python-2/
HARVARD
Srinivasulu Paranduru | Sciencx Friday October 17, 2025 » Decorators in Python., viewed ,<https://www.scien.cx/2025/10/17/decorators-in-python-2/>
VANCOUVER
Srinivasulu Paranduru | Sciencx - » Decorators in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/17/decorators-in-python-2/
CHICAGO
" » Decorators in Python." Srinivasulu Paranduru | Sciencx - Accessed . https://www.scien.cx/2025/10/17/decorators-in-python-2/
IEEE
" » Decorators in Python." Srinivasulu Paranduru | Sciencx [Online]. Available: https://www.scien.cx/2025/10/17/decorators-in-python-2/. [Accessed: ]
rf:citation
» Decorators in Python | Srinivasulu Paranduru | Sciencx | https://www.scien.cx/2025/10/17/decorators-in-python-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.