[Python-Dev] Class decorators

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Mar 30 05:09:54 CEST 2006


Phillip J. Eby wrote:

> the readability of @decorators on the outside of a class tends 
> to suck as the number of decorators and arguments increases.

So do decorators outside a function.

> What's more, I haven't seen anybody posting any counterexamples to show 
> that it doesn't suck for common use cases.  Indeed, at the moment I don't 
> even recall seeing any examples of class decorators being used without 
> arguments!

Well, here's how my use case would look if I had
class decorators:

   @IOClass
   class MyClass:
     ...

Does that count? My decorator wouldn't need any
arguments, because it looks inside the class for
all the information it needs. [1]

That's actually a general solution to Phillip's
concern: the decorator can always look for attributes
in the class (or the class's __dict__ if you don't
want them inherited) for large amounts of information
that wouldn't comfortably fit up the top. That's
an extra degree of freedom that we don't have with
functions.

- - - - -
[1] Actually I would probably give it one optional
     argument, the name to register under if different
     from the class name.

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | Carpe post meridiam!          	  |
Christchurch, New Zealand	   | (I'm not a morning person.)          |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+


More information about the Python-Dev mailing list