What is Decorator in python ?

Answer : A decorator in Python is a function that takes another function as an argument…

Decorator in python

  • A decorator in Python is a function that takes another function as an argument, while not changing the function being used as an argument.
  • Decorators dynamically alter the functionality of a function without using sub classes.
  • This is useful when you want to extend the functionality of functions and don’t want to modify them.
Python Decorators

Example:

def decorator(func):
	def wrapper():
		print "Hello Wikitechy"
		print func("Hai")
		print "How r you?"
 
	return wrapper
 
def some_random_function(hello):
	return hello
 
decorated_function = decorator(some_random_function)
decorated_function()
Leave a Reply

Your email address will not be published.

You May Also Like