<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Thanks for this very helpful post, Lie.&nbsp; I find decorators quite interesting and am always looking for new ways to understand and use them.&nbsp; Your trace function is fun, and I added it to my decorator library.&nbsp; In response to your point about event-handling in GUIs:<br><br>I haven't used pyqt that extensively yet, but it does have a decorator-based syntax for event-handling, which is more or less what you described.&nbsp; Its concept of events may be slightly different -- signals and slots -- but it's just a layer of abstraction on 'mouse-right-button-click-event,' so hopefully not unwelcome.&nbsp; I've been impressed with them so far.&nbsp; I hate GUI programming, and even got into programming in part to avoid GUIs, but I find it easy enough to make something with pyqt (using decorators) that a customized GUI is a decent alternative to just
 making command line scripts, especially if your working with anything visual that you'd like to display.<br><br>Cheers,<br>Soren<br><br>--- On <b>Tue, 10/26/10, Lie Ryan <i>&lt;lie.1296@gmail.com&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Lie Ryan &lt;lie.1296@gmail.com&gt;<br>Subject: Re: [Tutor] decorators (the "at" sign)?<br>To: tutor@python.org<br>Date: Tuesday, October 26, 2010, 12:30 PM<br><br><div class="plainMail">On 10/26/10 13:46, Alex Hall wrote:<br>&gt; Hi all,<br>&gt; Now that I am able to run the source code of an open source<br>&gt; application I hope to one day help develop, I am trying to understand<br>&gt; how it works. One thing I keep seeing is an at sign followed by a<br>&gt; word, usually (maybe always) immediately preceeding a function<br>&gt; definition. For example, and I know this exact code will not make much<br>&gt; sense, but it gives the
 idea:<br>&gt; class Bing(Messages, Updating, Dismissable):<br>&gt; <br>&gt;&nbsp; @set_index<br>&gt;&nbsp; def get_url(self, index=None):<br>&gt;&nbsp;&nbsp;&nbsp;return self.storage[index]['Url']<br>&gt; <br>&gt; What is the "@set_index" for? Specifically, what is the at sign doing?<br>&gt; Google was only able to provide me with a very vague idea of what is<br>&gt; going on, though it seems to crop up a lot in classmethod and<br>&gt; staticmethod calls (not sure about those either). I read PEP 318, but<br>&gt; it was not much help since I am coming at this having no idea what I<br>&gt; am looking at. The PEP did explain why I have never run into this<br>&gt; before, though - it is apparently specific to Python. I see this sort<br>&gt; of thing all over this source code so it seems like a good idea to get<br>&gt; exactly what it is for. TIA!<br><br><br>The decorator syntax is really just a shorthand, from this:<br><br>@decorator<br>def
 func(arg):<br>&nbsp; &nbsp; pass<br><br>is equivalent to:<br><br>def func(arg):<br>&nbsp; &nbsp; pass<br>func = decorator(func)<br><br><br>basically, a decorator is a function that takes an function as a<br>parameter/callable and (usually) returns another function/callable as<br>return value.<br><br><br>A slightly advanced usage of decorator:<br><br>def trace(func):<br>&nbsp; &nbsp; """ trace will print the func's name, the number of times<br>&nbsp; &nbsp; &nbsp; &nbsp; func have been called, the arguments it's called with,<br>&nbsp; &nbsp; &nbsp; &nbsp; and the return value of func whenever func is called.<br>&nbsp; &nbsp; """<br><br>&nbsp; &nbsp; # define an inner function<br>&nbsp; &nbsp; def _decorator(arg):<br>&nbsp; &nbsp; &nbsp; &nbsp; print ("The %sth call of %s with arg: %s" %<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (_decorator._numcall, func.__name__, arg))<br><br>&nbsp; &nbsp; &nbsp; &nbsp; _decorator._numcall += 1<br>&nbsp; &nbsp;
 &nbsp; &nbsp; ret = func(arg)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; print "finished", func.__name__, "returns:", ret<br><br>&nbsp; &nbsp; &nbsp; &nbsp; return ret<br><br>&nbsp; &nbsp; # this is used to store the number of times<br>&nbsp; &nbsp; # the decorated function is called<br>&nbsp; &nbsp; _decorator._numcall = 0<br><br>&nbsp; &nbsp; # disable the decorator when debugging is enabled<br>&nbsp; &nbsp; if __debug__: # or: return _decorator if __debug__ else func<br>&nbsp; &nbsp; &nbsp; &nbsp; return _decorator<br>&nbsp; &nbsp; else:<br>&nbsp; &nbsp; &nbsp; &nbsp; return func<br><br>@trace<br>def twice(arg):<br>&nbsp; &nbsp; return 2 * arg<br># the @trace makes it as if you do this:<br># twice = trace(twice)<br><br><br><br>$ # let's start the program<br>$ python decor.py<br>The 0th call of twice() with arg: 30<br>finished twice() returns: 60<br>The 1th call of twice() with arg: 3<br>finished twice() returns: 6<br>The 2th call of twice() with arg:
 4<br>finished twice() returns: 8<br>74<br>$<br>$ # now call it with debugging disabled:<br>$ python -O decor.py<br>74<br><br><br>another nifty use of decorator is for event handling for a hypothetical<br>GUI toolkit (I've yet to actually see a toolkit that uses this syntax yet):<br><br>@button.on_click<br>def shoot(button):<br>&nbsp; &nbsp; ...<br>@window.on_keypress('p')<br>def pause(key):<br>&nbsp; &nbsp; ...<br><br><br>other built-in functions designed for decorator syntax is @property,<br>@classmethod, and @instancemethod. Decorator can become extremely<br>powerful when combined with the descriptor protocol (.__get__, .__set__).<br><br>_______________________________________________<br>Tutor maillist&nbsp; -&nbsp; <a ymailto="mailto:Tutor@python.org" href="/mc/compose?to=Tutor@python.org">Tutor@python.org</a><br>To unsubscribe or change subscription options:<br><a href="http://mail.python.org/mailman/listinfo/tutor"
 target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br></div></blockquote></td></tr></table><br>