[Baypiggies] Examples of Handy Decorators

Glen Jarvis glen at glenjarvis.com
Sun Jul 18 18:46:55 CEST 2010


I'm trying to put together a very *very* basic introduction to decorators.
This has been done before (several times), so I know there is no silver
bullet.

What I'm focusing on is: examples of decorators -- from a user perspective
-- not how to write one, but how to use one.

Could you send any examples (off-list) (either real or fictional) that you
would use to help explain to a friend "This is a decorator for your use."
(again, currently avoiding the conversation on how to write one).

For example, Django has a "login_required" decorator that is handy so that
one doesn't have to understand how the login process works. Having the
decorator will either work if one is logged in; or will first prompt for a
login if one is not.

I can think of the logging example also -- where the decorator will log when
the function is being called. We've seen this presented as a defacto
standard on how a decorator is helpful.

Any examples that you can send -- even if it's a line or two -- would be
helpful. I think *explaining* decorators are hard. If you already get
decorators, then the talks will bore you to tears. If you don't, the
explanations never seem good enough.

The focus of my very quick presentation will be on the *why to use* (not how
to write) -- which seems to be the biggest stumbling block for those just
learning. I think this is mitigated by giving concrete small-but-not-toy
examples.

Once a person gets the *why*, I think its easier to get to the *how*
(although there can be some new concepts in there too)...

Let the brainstorming/ideas roll :)  pretty please :)


Cheers,


Glen
-- 
Whatever you can do or imagine, begin it;
boldness has beauty, magic, and power in it.

-- Goethe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20100718/e61837ad/attachment.html>


More information about the Baypiggies mailing list