Wednesday, 3 July 2013

Python Decorators

Python decorators are a convenient way to wrap functions around existing functions.
def mydecorator(fn):
    def wrap(*args, **kwargs):
        print "Before calling function"
        
        #call the original function (print_msg)
        #args will contain msg
        fn(*args, **kwargs) 
        
        print "After calling function"

    return wrap

@mydecorator
def print_msg(msg):
    print "Message: ", msg

if __name__ =='__main__':
    print_msg("Hello world");

Output:
Before calling function
Message: Hello world
After calling function

Since print_msg has @mydecorator above it, the interpreter will wrap print_msg with the mydecorator function. Arguments passed to print_msg will be available inside as *args and *kwargs in the wrap function inside mydecorator.

27 comments:

  1. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    Python Training in Bangalore

    ReplyDelete
    Replies
    1. IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes. IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble.Final Year Projects for CSE

      Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining .

      Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

      The Angular Training covers a wide range of topics including Angular Directives, Angular Services, and Angular programmability.Angular Training

      Delete
  2. Let's play together on the website of the online casino BGAOC. great gambling sites Do not miss the chance to win with us.

    ReplyDelete
  3. Attend The Python Training in Bangalore From ExcelR. Practical Python Training in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python Training in Bangalore.

    ReplyDelete
  4. Hello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.
    Data science Course Training in Chennai |Best Data Science Training Institute in Chennai
    RPA Course Training in Chennai |Best RPA Training Institute in Chennai
    AWS Course Training in Chennai |Best AWS Training Institute in Chennai

    ReplyDelete
  5. Such as very excellent information, it helps us to improve our mind knowledge. Keep on updating like this information!!

    Machine Learning Training in Bangalore

    ReplyDelete
  6. Attend The Data Analytics Courses in Bangalore From ExcelR. Practical Data Analytics Courses in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Courses in Bangalore.
    ExcelR Data Analytics Courses in Bangalore

    ReplyDelete
  7. I learned World's Trending Technology from certified experts for free of cost. I Got a job in decent Top MNC Company with handsome 14 LPA salary, I have learned the World's Trending Technology from Data Science Training in btm experts who know advanced concepts which can help to solve any type of Real-time issues in the field of Python. Really worth trying Freelance seo expert in bangalore

    ReplyDelete
  8. This was really one of my favorite website. ExcelR Machine Learning Course Pune Please keep on posting.

    ReplyDelete
  9. I have found it extremely helpful.Content of the Articles are fabulous, Thanks for all your Advice keep.updating more and more...
    python training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery

    ReplyDelete
  10. Very nice job... Thanks for sharing this amazing ExcelR Machine Learning Courses and educative blog post!

    ReplyDelete
  11. This is my first time visit here. From the tons of comments ExcelR Machine Learning Course on your articles.I guess I am not only one having all the enjoyment right here!

    ReplyDelete
  12. Thanks for sharing a very useful article. Am sure it helped to clear the doubts I had. Keep posting more. Also dont forget to check out our pages too.

    data science training in chennai

    ccna training in chennai

    iot training in chennai

    cyber security training in chennai

    ethical hacking training in chennai

    ReplyDelete
  13. I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job ExcelR Business Analytics Courses

    ReplyDelete
  14. Excellent blog!!! I got to know more useful information by reading your blog. Thanks for posting this blog.

    Python Training Institute in Pune
    Best Python Classes in Pune

    ReplyDelete
  15. Thanks for posting the best information and the blog is very helpful.python course in Bangalore

    ReplyDelete
  16. Really wonderful blog completely enjoyed reading and learning to gain the vast knowledge. Eventually, this blog helps in developing certain skills which in turn helpful in implementing those skills. Thanking the blogger for delivering such a beautiful content and keep posting the contents in upcoming days.

    data science certification in bangalore

    ReplyDelete
  17. This information is really awesome thanks for sharing most valuable information.

    DevOps Training Institute in Pune

    ReplyDelete