Decorators in Python with Examples
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Program 1
def mydecorate(myfactorial): def mywrapper(): print("***Program for Factorial*******") n=int(input("Enter a Number")) myfactorial(n) print("***End of Program for Factorial*******") return mywrapper def factorial(n): f=1 while(n!=0): f=f*n n=n-1 print("Factorial is ",f) fun1=mydecorate(factorial) fun1() # def mydecorator(myfunction): # Decorator # def mywrapper(): # print("*********This is begin of function**********") # myfunction() # Orignal function # print("-----------This is end of function------------------") # return mywrapper # def myfun(): # print("This is my orignal function Myfun ") # fun1=mydecorator(myfun) # fun1()
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google