Hi
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.

Okay.. i think i get it, but why use it? Is it just to make your code more clean?

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.

commented: Thanks for the good info! +6

Okay thats a lot clearer now, cheers everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.