Saturday, August 7, 2010

Python Decorators

Thanks to this answer on Stack Overflow, I've finally understood decorators. Up until now I understood the syntax, I understood what decorators did, but it was only after I hand coded all the examples in that post that I really "got" what and how decorators work.

I understand now that a decorator simply takes a function and wraps it or its return value in another function which it returns afterwards. I guess this is useful if you have a bunch of different functions that you want to have some common functionality. Instead of writing the same code in each function, you can write a decorator and decorate each of the functions.

Next, I'll just have to use a decorator in a real-life situation, so that I can remember it better.

No comments:

Post a Comment