decorators

Carsten Haese carsten at uniqsys.com
Wed Nov 8 16:43:26 EST 2006


On Wed, 2006-11-08 at 12:37 -0800, John Henry wrote:
> I must be very thick.  I keep reading about what decorators are and I
> still don't have a good feel about it.  See, for example:
> 
> http://alex.dojotoolkit.org/?p=564
> 
> and:
> 
> http://soiland.no/software/decorator
> 
> What exactly do I use decorators for?

Decorators are a tool for executing common code when a function is
defined and/or when it's called. This reduces code duplication by
allowing you to factor out commonly performed steps.

If you find yourself writing different functions that share a lot of
setup/teardown code, or if you find yourself doing a lot of repetitive
housekeeping for each function you're defining, decorators eliminate
this duplication.

You've already been given a bunch of different examples, so I won't bore
you with more contrived examples. If none of the examples make you say
"wow, I could use this" and if you don't find yourself writing
repetitive setup/teardown/housekeeping code, you can probably live quite
comfortably without using decorators.

-Carsten





More information about the Python-list mailing list