Thanks to all for the explanations. I think I understand how this works, basically. I doubt I will use the concept anytime soon, but I think I get it enough to follow what is happening in the source code of the application I am examining.
On 10/27/10, Siren Saren <sire...@yahoo.com> wrote: > Alex, > > Many people have trouble wrapping their minds around decorators. I couldn't > find a decent explanation either until reading an allegedly 'advanced' > python book (b/c 'advanced' is in the title, I guess). > > An easy explanation might be sufficient, if you don't intend to use them > yourself. A decorator is a way of changing a function's behavior. Most of > the time you see a decorator, the person using it could have merely > rewritten the function or method itself and given it some additional > capability or modified it in some way. But, as you may have noticed, > programmers prefer to 'abstract' when they can, so they can avoid > 'duplicating code.' (These are buzzwords you'll encounter a lot). > > Let's say your program needs to set an index in a lot of different > functions, and let's further imagine that setting an index is either more > than a line of code or that the index itself may change over the development > cycle, or that the index will vary according to some simple pattern > consistently defined in these other functions. > > To avoid writing the same code over and over and having the value of index > set in many different places, the developers chose instead to write the code > once and refer to it in all the other functions through a decorator, which > takes all the functions, modifies them so they get the index values they > need, and sets them back in their places (more or less). > > If you want the more complicated answer, I think I can take a reasonable > shot at showing how this works too and making an example. But you may just > want a general description. Also, I'm only about 4 months into learning to > program so you may prefer a more expert opinion. > > Cheers, > > Soren > > --- On Tue, 10/26/10, Alex Hall <mehg...@gmail.com> wrote: > > From: Alex Hall <mehg...@gmail.com> > Subject: [Tutor] decorators (the "at" sign)? > To: "tutor" <tutor@python.org> > Date: Tuesday, October 26, 2010, 2:46 AM > > Hi all, > Now that I am able to run the source code of an open source > application I hope to one day help develop, I am trying to understand > how it works. One thing I keep seeing is an at sign followed by a > word, usually (maybe always) immediately preceeding a function > definition. For example, and I know this exact code will not make much > sense, but it gives the idea: > class Bing(Messages, Updating, Dismissable): > > @set_index > def get_url(self, index=None): > return self.storage[index]['Url'] > > What is the "@set_index" for? Specifically, what is the at sign doing? > Google was only able to provide me with a very vague idea of what is > going on, though it seems to crop up a lot in classmethod and > staticmethod calls (not sure about those either). I read PEP 318, but > it was not much help since I am coming at this having no idea what I > am looking at. The PEP did explain why I have never run into this > before, though - it is apparently specific to Python. I see this sort > of thing all over this source code so it seems like a good idea to get > exactly what it is for. TIA! > > > -- > Have a great day, > Alex (msg sent from GMail website) > mehg...@gmail.com; http://www.facebook.com/mehgcap > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > -- Have a great day, Alex (msg sent from GMail website) mehg...@gmail.com; http://www.facebook.com/mehgcap _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor