I have been using classes for quite a while but there is still one concept yet that i havent ever quite understood and that is decorators. I have noticed Gribouillis uses them a lot so in some ways this question is directed at him. I would like to know in plain english what decorators do and how to use them, when to use them and why you use them. I found that all that i looked at in books and on the net didnt really tell me that much so any insight would be lovely.

9 Years
Discussion Span
Last Post by Paul Thompson

I saw someone say this on another thread and I want to copy it, so here goes. I have never used decorators before, so I am uniquely qualified to speak on the subject.

Based on the example in the threat it seems like it would make your code less readable. Sure, there may be fewer lines, but the program flow isn't as obvious which might mean that you need to comment more. Am I wrong?


Hello again and merry christmas ! In fact I don't use decorators so much. The decorators I use most often are the builtin decorators @property , @staticmethod and @classmethod . I discovered decorators in the attached slides of pycon 2005, which don't seem to be available anymore at python.org. Decorators are useful mainly as a way of 'typing' functions, when you want to apply the same transformation to a collection of functions or methods. For example you want to define the concept of 'a function which checks its arguments' or 'a function which traces its calls' . You should check the python decorator library http://wiki.python.org/moin/PythonDecoratorLibrary. There is also PEP 318 which introduces decorators. I also found a nice @consumer decorator in pep 342.

Votes + Comments
Thanks for the good info!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.