[Python-Dev] Class decorators
Phillip J. Eby
pje at telecommunity.com
Wed Mar 29 05:06:05 CEST 2006
At 02:55 PM 3/29/2006 +1200, Greg Ewing wrote:
>Phillip J. Eby wrote:
> > http://mail.python.org/pipermail/python-dev/2004-March/043462.html
> > Or more precisely, the subsequent discussion and examples convinced me
> > putting class decorators on top of the class was bad for readability, vs.
> > putting them in the body just after the docstring.
>I just looked at that discussion, and I didn't see any
>argument to refute the thesis that if decorators-on-top
>is good enough for functions, it should be good enough
>In fact it seems to have been written before the on-top
>decorator syntax existed, so there's no discussion of
>the issue at all.
>Seems to me that, since we now already have @-decorators
>for functions, a *very* good reason will be needed for
>requiring a completely different syntax to get exactly
>the same effect for classes.
And here it is: because the use cases for class decorators are
different. I routinely use them with things that take numerous keyword
arguments, but this isn't nearly as common of a scenario for function
decorators. Also, class decorators are far more likely to be just
registering the class with something -- which means they don't deserve so
prominent a location as to obscure the class itself.
ObDisclaimer: this is my personal experience and opinion. Others may have
different use cases in mind. I'm just pointing out that if @decorator
support were added for classes, I wouldn't use it, because it's not
actually an improvement over what I'm doing now. Function decorators,
OTOH, *are* an improvement over what I did before, so I use them.
More information about the Python-Dev