DECORATORS: =========== Decorator is a function that takes another function as an argument add some kinds of functionality without modifying the original functionality and returns a function. Syntax: def decorator(func): add some functionality return func 1) how the function can take a function as an argument: ======================================================= Syntax: def function-name(func): implementation # Sending a function as an argument to another function: def do_it_again(func): func() func() func() def greets(): print("Hi") print("Good Morning") print("Welcome to Ashok IT.") do_it_again(greets) 2) how the function can return a function ========================================== Syntax: def function-name(function(optional)): implementation return function # Function returning a function: def return_upper(): return str.upper to_upper = return_upper() # function-aliasing """ renaming the function or define the another name to the function which we have defined is called as "function aliasing". """ result = to_upper("object oriented programming language") print(result) # print(to_upper("object oriented programming language")) 3) inner function ================= writing a function inside the another function. Syntax: def outer(): def inner(): # local function to outer() implementation inner() inner() # inner function def parentFunction(): print("This is the implementation of parentFunction()") # 1 def childFunction1(): print("This is the first child's implementation.") # 3 def childFunction2(): print("This is the second child's implementation.") # 2 # childFunction1() childFunction2() childFunction1() parentFunction() # childFunction1() ============================================== Creating of a Decorator: ======================== Syntax: func = decorator(func) # Creation of Decorator: def decorator(function): def wrapperFunction(): print("This is printed before the function is called.") function() print("This is printed after the function is called.") return wrapperFunction def greetings(): print("Hi") print("Good Morning.") print("Welcome to Ashok IT") greetings = decorator(greetings) greetings() ================================================== Syntactic Decorator: ==================== @decorator: def function(arg1, arg2, arg3,...): pass # Syntactic Decorator def decorator(function): def wrapperFunction(): print("This is printed before the function is called.") function() print("This is printed after the function is called.") return wrapperFunction @decorator def greetings(): print("Hi") print("Good Morning") print("Welcome To Ashok IT.") greetings()