Decorators

Colin J. Williams cjw at sympatico.ca
Sat Aug 7 13:47:45 EDT 2004


Christopher T. King suggested that "we're trying to kill too many birds 
with one stone".
 
http://groups.google.com/groups?q=stone+birds+group:comp.lang.python&hl=en&lr=&ie=UTF-8&selm=Pine.LNX.4.44.0408050856390.31290-100000%40ccc9.wpi.edu&rnum=1

He goes on to suggest three needs which decorators serve.  Are these the 
only purposes which are envisaged for decorators?

Most of the discussion has focused on syntax.  I would like to see some 
clarification of the purpose and meaning of a decoration.

The latest version of PEP 318 has a Motivation section which talks of 
transforming a method or function.  The possible purposes of the 
transformation is not so clear.

The use of the term decorator is questionable.  In everyday usage, to 
decorate is to furnish with adornments.  In this sense, we could say 
that a docstring is a decorator, but is that true of a transformation.
It seems desirable that we choose words which are as close as possible 
to their everyday usage.

Example 4 in the PEP 318 illustrates the enforcement of function/method
argument and return checking.  Is it envisaged that the accepts and 
returns functions become part of __builtins__?  The code seems to assume 
that the interpreter will divert the func arguments to accepts and the 
return object to returns.  Is this correct?  If so, how many trigger 
words are implemented?

Some syntactic options are provided in:
    http://www.python.org/moin/PythonDecorators
I am inclined to support option D.  The decorator follows the function 
definition and is indented, indicating that the decorator qualifies the
decorated and is, in a sense subordinate to it. It doesn't use @.

Steve Bethard sets out the syntactic choices clearly:
 
http://groups.google.com/groups?q=bethard+decorator&hl=en&lr=&ie=UTF-8&selm=d11dcfba.0408060919.10c7cdd6%40posting.google.com&rnum=1

My choices would be:

Indicator:  Keyword preferred, but not | (vertical bar), as it looks too 
much like 1 or l.

Location: First line of the body.  The decorators are subordinate to the 
signature and indented like the docstring.

List notation: As ListType, with a line per entry.

Indented: Yes, no block.

Clearly, I don't fully understand PEP 318, but I wonder at times is 
decorators aren't a solution in search of a problem.

Colin W.




More information about the Python-list mailing list