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

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *