DECORATORS: =========== Docstring: ========= # Docstring # used to define the description about any block (class, function, method etc.) # during the debugging. # can be defined with """ """ def fun1(t): """ This function is defined to print greetings like: "good morning" "good afternoon" "good evening" and "good night". """ if t >= 0 and t < 12: print("Good Morning.") elif t >= 12 and t < 16: print("Good Afternoon.") elif t >= 16 and t < 20: print("Good Evening.") else: print("Good Night.") fun1(6) print(help(fun1)) print(help(print)) Preserving the Original Name and Docstring of the 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 greets(): """ This function says hello when we called. """ print("Hello All.") print(greets.__name__) help(greets) ==> To preserve the identity of function and the documentation string of the function, we can use "decorator". wraps() ==> functools import functools def decorator(function): @functools.wraps(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 greets(): """ This function says hello when we called. """ print("Hello All.") print(greets.__name__) help(greets) ====================================== Reusing Decorator: ================== 1) create a python file for defining decorator ex: decorators.py ================== import functools def iterativeFunction(function): @functools.wraps(function) def wrapperFunction(): print("This is printed before the function is called.") function() print("This is printed after the function is called.") return wrapperFunction 2) Import that decorator into application/anoter python file where we want to reuse it. Syntax: from decorator-module-name import decoartor-name from decorators import iterativeFunction @iterativeFunction def greets(): print("Hello All.") @iterativeFunction def welcome(): print("Welcome To Ashok IT.") greets() welcome() =============================================== Decorators With Parameters: =========================== wrapper(with parameters) Syntax: def wrapper(*args, **kwargs) # Decorator with parameters import functools from Decortaors.Day_02.decorators import iterativeFunction def iterateFunction(function): @functools.wraps(function) def wrapper(*args, **kwargs): function(*args, **kwargs) function(*args, **kwargs) return wrapper @iterateFunction def greets(name): print("Hello",name) # greets("Rakesh") pname = "Suresh" greets(name = pname) ============================================= Returning values from Decorator Function: ========================================= import functools def decorator(function): @functools.wraps(function) def innerFunction(*args, **kwargs): return function(*args,**kwargs) return innerFunction @decorator def addition(n1,n2,n3): print("The sum of {n1}, {n2} and {n3} is = ") return n1 + n2 + n3 print("The sum is = ",addition(11,111,1111)) ===================================================== Do we define the Decorators with Arguments?: ============================================ Yes It's all based on the inner function of the decorator. If the inner function of the decorator with parameters ==> we can send arguments to decorator. otherwise ==> we can't ======================================================== Chaining Decorators: ==================== Defining multiple decorators to a single function is called as "Chaining Decorator". # Chaining Decorator import functools def splitString(function): @functools.wraps(function) def innerFunction(*args,**kwargs): return function(*args, **kwargs).split() return innerFunction def toUpper(func): @functools.wraps(func) def wrapperFunction(*args, **kwargs): return func(*args,**kwargs).upper() return wrapperFunction @splitString @toUpper def String(data): return data print(String("object oriented programming language"))