[Python-Dev] def ... decorate
Roman Suzi
rnd at onego.ru
Fri Aug 13 18:24:21 CEST 2004
On Fri, 13 Aug 2004, Skip Montanaro wrote:
>with
>
> def p_statement_expr:
> 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]
+ doesn't surprise Python programmer, because it is like try-except, etc
+ reads a natural language (with implicit "with" after "f")
+ doesn't require any new keywords or symbols and "prefix" operators
+ is explicit about transformation
+ no additional indentation
- grepping for defs now require more effort, so probably second name should
be allowed after "from" or exactly this.
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 p_statement_expr 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