[Python-Dev] def ... decorate (original) (raw)

Roman Suzi rnd at onego.ru
Fri Aug 13 18:24:21 CEST 2004


On Fri, 13 Aug 2004, Skip Montanaro wrote:

with

def pstatementexpr: staticmethod grammarrule('statement : expression') version("Added in 2.4") deprecatedmethod type(None) decorate (self, p): """docstring here""" print p[1]

Bingo!

Just replace decorate with "from" and the nice syntax is found:

def f:
    staticmethod
    grammarrule('statement : expression')
    version("Added in 2.4")
    deprecatedmethod
    type_(None)
from self, p:
    """docstring here"""
    print p[1]

As a variant, docstring could be moved to the upper part.

I'd also liked being able to write:

def f: staticmethod; grammarrule('statement : expression') version("Added in 2.4"); deprecatedmethod; type_(None) from x, y: pass #...

and:

def f: staticmethod from x, y: return x+y

It seems different enough from other solutions that I thought it worth tossing out. I didn't see it mentioned in the PythonDecorators moin page.

Read it something like "define a function named pstatementexpr using a bunch of functions to decorate the basic function". It solves a couple problems: 1. "def" introduces the function definition instead of an arbitrary number of @-expressions. 2. There is no extra indentation of the main body. 3. The name of the function is known early on. 4. "def"/"decorate" pair up visually much the same as "try"/"except" or "if"/"then", though they don't represent alternative blocks of code to be executed. On the minus side it introduces a vertical separation between the function name and parameter list and introduces a new keyword, "decorate".

From a parsing standpoint I think it will work. You'll see either a colon or a left paren after the function name to distinguish between the two types of function definition. I'm not sure if a token needs to be used to separate the various decorator functions or if requiring a newline and indentation is sufficient. Skip


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/rnd%40onego.ru

Sincerely yours, Roman Suzi

rnd at onego.ru == My AI powered by GNU/Linux RedHat 7.3



More information about the Python-Dev mailing list