[Python-Dev] Pep 318 - new syntax for wrappers

Jewett, Jim J jim.jewett at eds.com
Wed Mar 3 10:20:08 EST 2004

I think everyone agrees that decorators are useful, and the 
current idiom is awkward; the only real argument is over how 
to improve it.  Proponents want something easy to type; others 
want something explicit.  

(1)  Eventually, there will be another good idea.

Therefore, we should define a general extension syntax, 
rather than a special case for this particular extension.

    def func(args) [wrapper1, wrapper2]:

uses up the syntax forever.

    def func(args) mods [wrapper1, wrapper2]:

allows later extensions like

    def func(args) alias [wrapper1, wrapper2]:
    def func(args) compiler_hints [wrapper1, wrapper2]:
    def func(args) my_domain_special_use [wrapper1, wrapper2]:

(2)  Wrappers are not essential to understanding functions, 
and should not pretend to be.  

In most languages, classifiers are always relevant.  Defaults
might save you from *writing* "int", "public", or "synchronized", 
but the classification still happens.  If you want your code 
to be robust, you need to at least understand the possible
classifiers, and when to use them.  This means you need to
learn something about each of them soon after "Hello World".

In python, these extensions really are optional.  The syntax 
should make it clear that newbies can ignore the whole 
construction, and do not need to know any specific wrappers.

    def func(args) [wrapper1, wrapper2]:

suggests that the extensions are part of basic function syntax.
Lack of documentation for wrapper1 and wrapper2 (which may not 
be part of the core language) presents an extra barrier.  So
does the fact that this is a new meaning for [], at least under
the current patch.  People can learn to ignore it -- but only if
they have already committed to python.  Each new barrier to 
entry makes a newbie more likely to give up before starting.

    def func(args) decorated_by [wrapper1, wrapper2]:

looks optional.  Shorter keywords are OK too, if they're still
clear; these were the shortest suggestions:

    def func(args) mods [wrapper1, wrapper2]:  #MODifierS
    def func(args) as [wrapper1, wrapper2]:    #!but import as=alias
    def func(args) X [wrapper1, wrapper2]:     #x=Transform

Also note that using a keyword (such as "mods", "as", or "X") 
makes it easier to replace literal "[ ]" with a more general 
(sequence) expression later.


More information about the Python-Dev mailing list