[Tutor] what does the "@" operator mean?

Kent Johnson kent37 at tds.net
Tue Dec 16 14:49:52 CET 2008


On Mon, Dec 15, 2008 at 9:33 PM, Marc Tompkins <marc.tompkins at gmail.com> wrote:

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

FWIW my attempt at explaining decorators is here:
http://personalpages.tds.net/~kent37/kk/00001.html

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

It's hard to find a simple example of decorators that is not
contrived. The real-world cases are often too complex for an
introduction. I cite a few examples in my article. Some other
examples:
Django's login_required decorator:
http://docs.djangoproject.com/en/dev/topics/auth/#the-login-required-decorator

In the standard lib, contextlib.contextmanager makes it easy to define
context managers (objects that work with the 'with' statement):
http://docs.python.org/library/contextlib.html#contextlib.contextmanager

and functools.wraps helps to make well-behaved decorators ;-)
http://docs.python.org/library/functools.html#functools.wraps

I told you it was hard to find simple examples!)

Kent


More information about the Tutor mailing list