Alternative decorator syntax - keyword choice

Colin J. Williams cjw at sympatico.ca
Sun Aug 22 11:01:58 EDT 2004


Jeremy Bowers wrote:

> On Sat, 21 Aug 2004 21:51:09 -0700, Robert Brewer wrote:
> 
>>I'd be most interested to hear arguments _against_ "declare", if there
>>are any.
> 
> 
> My first objection would be that decorators aren't "declarations"; that is
> one use but not the only one.
> 
Another is that the word doesn't give a clue as to what is happening.

> The flip side is that I doubt that any one word will ever capture as a
> reasonable verb what they are doing, so I'm not sure this argument is
> meaningful, as it applies equally to all proposals, up to and including
> "decorate". ("Decoration" is the mechanism, but it is not always the
> result.)
> 
> I'd have to say "transform" is probably the best candidate here; while not
> all uses of decorators are transforms (like attaching pieces of metadata),
> it at least brings to mind the correct implementation, which is the
> passing of the method through a function. 
>
> "declare" brings to mind attaching a piece of data to the function or
> making a statement about the function to the compiler, without changing
> the underlying function. Ditto "apply".
> 
> "decorate" brings either nothing to mind, or some vision of the Design
> Pattern which really isn't right.
> 
> "as" seems to imply some sort of syntactic change, and only works AFAICS
> for staticmethod and classmethod. "as: author('Steve')" doesn't make much
> sense.
> 
> To sum up, *all* keywords are insufficient, and I think there are only two
> choices, depending on the goal: "transform" to give a hint about what is
> really happening, or "decorate" on the grounds that there *is* no
> ready-made keyword and it is reasonbly easy to attach whatever Pythonic
> connotations to the word we choose; witness "generators".
> 
> 
> (Apologies if this has been mentioned before; I've been trying to keep up
> but I have failed.)

The overall effect of implementing PEP 318 is transformative rather than 
decorative.  Thus, "transform" is a better choice. It could be argued 
that even "author('Steve')" is a transformation, in that it could well 
amend an existing notation.

Three issues which have not been discussed much are:
     1. the purpose of the docstring in J2 preceding the funcdef.  It it
        to describe the transformers or the function?
     2. the placement of the extra code relative to the funcdef.
        (incidentally, I don't find docstring in the Python grammar)
        It seems to me that the reading flow would be better if it were
        to follow the funcdef, so that the funcbody would become
                 funcbody ::= [[newstuff] body].
     3. whether the PEP 318 processes are to apply to classes as well
        as functions.

Colin W.




More information about the Python-list mailing list