[Python-Dev] method decorators (PEP 318) (original) (raw)

Greg Ewing greg at cosc.canterbury.ac.nz
Sun Mar 28 22:07:56 EST 2004


Robert Mollitor <mollitor at earthlink.net>:

Not only would we want "synchronized(classmethod(f))" to work, but we would probably want "classmethod(synchronized(f))" to work identically. This is not easy unless either the implementation of each was aware of the other (or at least the possibility of the other), or all transformers must be written to a tight specification.

For that to work, all transformers would have to be prepared to receive a descriptor instead of a callable, extract the underlying callable, transform that, and then wrap the result back up in a descriptor of the same type.

While that could be done for specific types of descriptor, it couldn't be done in general because there's no standard for callable-wrapping descriptors concerning how to extract the wrapped callable.

Also, it would still only be possible to have at most one descriptor-creating transformer in the chain. That's probably unavoidable, since in general different kinds of descriptors are going to be fundamentally incompatible with each other. This probably isn't a disadvantage, since it wouldn't make sense to combine such descriptors anyway (e.g a method can't be both a classmethod and a staticmethod).

It seems we have two quite different kinds of transformer here: (1) those that take a callable and return another callable; (2) those that take a callable and return a descriptor.

The differences between them are substantial. Type 1 transformers are easily chained, and it seems people will often want to do so. It also seems that for the most part it will be easy to design them so that transformers with orthogonal effects can be chained in any order.

On the other hand, it seems that it will usually make sense only to have one transformer of type 2 in any given definition, and it must be applied last.

Particularly considering the latter restriction, I'm wondering whether these should be treated differently in the syntax. Maybe something like

def [type2trans] foo(args) [type1trans, type1trans...]: ...

or

def foo [type2trans] (args) [type1trans, type1trans...]: ...

This might also help satisfy Guido's concerns about the positioning of 'classmethod' and 'staticmethod', since, being type 2 transformers, they would go up front.

Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg at cosc.canterbury.ac.nz +--------------------------------------+



More information about the Python-Dev mailing list