[Python-Dev] decorators: If you go for it, go all the way!!! :) (original) (raw)

Simon Percivall s.percivall at chello.se
Thu Aug 26 13:50:22 CEST 2004


On 2004-08-26, at 12.58, alex_nanou at cs.msu.su wrote:

the inability to decorate anything but a function, is quite a limitation, though I must agree that decorating classes is not such a common task, but using the syntax to decorate attributes would indeed prove useful (in both the readability department and the uniformity of code).. [...]

and here are a couple of usage examples: the desired decorator syntax: ---cut--- class A(object): @intonly i = 123 --uncut-- the current usage syntax: ---cut--- class A(object): i = intonly(123) --uncut--

IMHO, this would be bad.

As I see it, the need for a special syntax for decoration, in this case a prefix syntax, comes mainly from the fact that method/function and class declaration is just that, declarations. You can only pass the entity to a decorating function after it has been bound. Hence the irritating need to type the entity name three times, once declaring, once re-binding and once passing to the decorating function. This just isn't an issue here. If you create an instance of some type, such as the integer literal in your example, you can, just as you show, pass it to a function directly. This is truly the best way, and the decoration is unnecessary.

Also, I don't agree it increases readability. The @ character marks something special, something you might really need to see to understand what is defined with a function/method or class. That's why re-binding that entity afterwards isn't good enough: you might miss it. Again, this isn't an issue here.

//Simon



More information about the Python-Dev mailing list