Untitled Document
Decorators
1. Introduction Click Here
# What is Decorator ?
# A decorator is a function taking another function as an argumnet add its functionality without altering the Original function 
# i.e the function which is passed as argument to a decorator
       
       
2. Program - Simple Decorator Example Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Simple Decorator Example 
       
def decorator_function(original_function):
	def wrapper_function():
		return original_function()
	return wrapper_function


def display():
	print('Display function ran')

decorated_display = decorator_function(display)

decorated_display()
       
3. Program - Another Example - Here we have added one print statement inside wrapper function Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Another Example - Here we have added one print statement inside wrapper function 
       
def decorator_function(original_function):
	def wrapper_function():
		print('wrapper executed this before {}'.format(original_function.__name__))
		return original_function()
	return wrapper_function


def display():
	print('Display function ran')

decorated_display = decorator_function(display)

decorated_display()
       
4. Program - Actual Syntax of Decorators in programming Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Actual Syntax of Decorators in programming 
       
def decorator_function(original_function):
	def wrapper_function(*args, **kwargs):
		print('Wrapper executed this before {}'.format(original_function.__name__))
		return original_function(*args, **kwargs)
	return wrapper_function

@decorator_function
def display():
	print('display function ran')

@decorator_function
def display_info(name,age):
	print('display_info ran with arguments ({}, {})'.format(name,age))

display_info('John', 25)
display()
       
Untitled Document