Decorators in Python

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

#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

Total
0
Shares
Leave a Reply

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

Previous Post
colors:-nono-la-grinta-–-love-you-|-a-colors-show

COLORS: Nono La Grinta – LOVE YOU | A COLORS SHOW

Next Post
enterprise-generative-ai-tools-that-actually-work

Enterprise generative AI tools that actually work

Related Posts