[Python-Dev] Re: @decoration of classes

Scott David Daniels Scott.Daniels at Acm.Org
Mon Mar 28 22:52:21 CEST 2005

Michael Chermside wrote:
> Josiah Carlson writes:
>>...While I have not used it often, I have done the equivalent of decorating
>>classes; it is as natural (though perhaps not quite as useful initially)
>>as decorating functions,
> But a few *good* use cases would change my mind.

Until this weekend, I really had no idea what a good use case for
class decorators would look like.  However, I stumbled upon something
interesting.  I was refactoring Kirby Urner's "hypertoons" code this
weekend and hit upon an interesting use for decorators.  On reflection,
this might well be a candidate for class decorators.  The unusual thing
is that it does nothing to the decorated function; it simply stores it
in a data structure.  The "converter" decorator builder can be used for
data values, including classes, but there is an advantage to having
them at the top of the definition.

Using such decorators looks like:

     @converter('inch', 'foot')
     def someconversion(...

     @converter('foot', 'yard')
     def anotherconversion(...

     @converter('yard', 'furlong')
     def yetanotherconversion(...

Classes can be put into the data structures with:

     converter('flour', 'bread')(BakingClass)

_But_ (at least for the app I was fiddling with) decorating at the top
of declaration helps show the purpose of the class.

Have a look at:


and see what you think.

-- Scott David Daniels
Scott.Daniels at Acm.Org

More information about the Python-Dev mailing list