[Python-Dev] pep 318, Decorators for Functions, Methods and Classes

Bob Ippolito bob at redivi.com
Sat Aug 7 20:18:51 CEST 2004


On Aug 7, 2004, at 2:02 PM, Simon Percivall wrote:

>> Guido van Rossum wrote:
>> >
>> > Some people have argued that it should work for classes (Java and C#
>> > decorators work for any declaration).  I've never been convinced,
>> > although I'm not dead set against it either.
>> >
>> > It would seem that the functionality isn't quite as useful there; 
>> you
>> > can get most of the same effects with metaclasses.  I'm not aware of
>> > any idiom that takes a class and passes it through some kind of
>> > wrapper or transformation *after* the class declaration; if somebody
>> > is using this, it surely must be a very rare use indeed.
>> >
> I just wanted to say that I believe it should be allowed to decorate
> classes. There's not reason enough (reading the thread linked to by the
> PEP) to limit decorators this way. Like said several times in that
> thread, metaclasses are harder (to write and to understand) than
> decorators.
>
> Also, considering that you said you would have actually wanted
> docstrings above the definition of a function, wouldn't this apply
> to classes as well?
>
> Anyway, it's an artificial limit and it would be better to be able
> to test it out during the alpha.

I've always been +1 for allowing them on classes too for the reasons 
that you state, and I would use them if they were there.  However, 
since we do have metaclasses and I know how to use them it's not nearly 
as important to me as function decorators and I'd rather not get in 
another big thread about it.

Note that you could make a docstring decorator that does just this..

def doc(docstring):
	def doc(fn):
		fn.__doc__ = docstring
		return fn
	return doc

@doc("""foo does nothing and takes no arguments""")
def foo():
	pass

It could of course work the same way for classes too.

-bob


More information about the Python-Dev mailing list