On Mon, Dec 15, 2008 at 5:03 PM, Alan Gauld <alan.ga...@btinternet.com> wrote: > > "Marc Tompkins" <marc.tompk...@gmail.com> wrote > >> If you're just starting out in Python, decorators can be hard to get >> your head around... > > I've been using Python for oover 10 years and still find decorators > hard to get my head around! :-) > > I confess I'm not a fan, they go against the Python spirit of > explicit is best in my opinion. If I'm calling a function I like to > know I'm calling a function... I know they make the code look > pretty but IMHO they are a pain to debug and I'm never totally > convinced I've got it exactly right. > > Alan G > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor >
I'm sorry I left it as flat as I did - that Dr. Dobbs article is a pretty good explanation, and there's an article somewhere in the Effbot bookshelf that does a decent job - but I couldn't find either one at that moment. I've only been using Python for a couple of years now, but my experience so far is the same as yours: decorators make my head hurt. Even in the Dr. Dobbs article, the examples seem horribly contrived. There must be plenty of programmers who use decorators all the time and would feel lost without them, but I have yet to see a compelling use case. (It's always something like "Here's another useless, made-up situation. We could handle it in a straightforward way, but wouldn't it be cooler if we used a decorator instead?") Actually, I'm being unnecessarily harsh: I can imagine a theoretical case, where there is some operation you wish to apply to several functions, and you don't want to write the code more than once. (Wrapping a timer around functions comes to mind.) But I've never run across a situation where a decorator actually seemed like the best way to do it, and (almost) all of the articles on the subject feel like they were written to check an action item off the editor's to-do list: this book won't be complete unless we mention decorators, so better slop something together. Does anybody who reads this list use decorators and have a nice word to say about them? I'd be interested to hear it. -- www.fsrtechnologies.com p.s. - Is anybody else as sick of the phrase "syntactic sugar" as I am? _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor