Are decorators really that different from metaclasses...

Paul Morrow pm_mon at yahoo.com
Thu Sep 2 15:12:12 EDT 2004


Jeff Epler wrote:
> On Thu, Sep 02, 2004 at 07:03:50AM -0400, Paul Morrow wrote:
> 
>>>You also add a new constraint not expressible directly in the Grammar
>>>file.
>>
>>It would be easy to write a BNF expression that states that the optional 
>>docstring is followed by 0+ assignments to magic variables.  So what are 
>>you referring to here?
> 
> 
> Please show me the Grammar rule you have in mind.  Here's the relevant
> part of the current Grammar file, for reference:
> 
> funcdef: [decorators] 'def' NAME parameters ':' suite
> suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
> ... (simple_stmt and stmt both include expr_stmt as an alternative)
> expr_stmt: testlist (augassign testlist | ('=' testlist)*)
> 

Something like this.  Now I'm not sure where you got that grammar text, 
but working from the one at http://docs.python.org/ref/grammar.txt I'd do

   funcdef ::=
     "def" funcname "(" [parameter_list] ")"  ":" funcsuite

   funcsuite ::=
     [oneline_funcdecl] stmt_list NEWLINE
     | NEWLINE INDENT [multiline_funcdecl] statement+ DEDENT

   magic_identifier ::=
     "__" (letter|digit) (letter|digit|"_")*  "__"

   multiline_docstring ::= stringliteral NEWLINE

   multiline_funcdecl ::=
     [multiline_docstring] multiline_property_setting*

   multiline_property_setting ::=
     magic_identifier "=" expression NEWLINE

   oneline_docstring ::= stringliteral ";"

   oneline_funcdecl ::= [oneline_docstring] oneline_property_setting*

   oneline_property_setting ::=
     magic_identifier "=" expression ";"


Note that I'm calling the assignments to magic variables 'property 
settings' and a function's docstring plus it's property settings would 
constitute the function's declaration (funcdecl).  The BNF nonterminals 
that start with 'oneline_' all appear on the same line, separated by a 
semi-colon.  Those that start with 'multiline_' all appear on separate 
lines.

Also note that the definition of 'suite' you posted doesn't show the 
constraint that a docstring can only be a string literal.  For that 
matter it doesn't even formally recognize a docstring as a special 
component of the function suite.


> [1] __*__
>     System-defined names. These names are defined by the interpreter and
>     it's implementation (including the standard library); applications
>     should not expect to define additional names using this convention.
>     The set of names of this class defined by Python may be extended in
>     future versions. See section 3.3, ``Special method names.''
>             -- http://docs.python.org/ref/id-classes.html
>     
>     My function attribute was not system-defined, but
>     application-defined.
> 

But according to your footnote, you're not permitted to define such 
names: "applications should not expect to define additional names using 
this convention."

So it seems that if you technically aren't free to use that syntax for 
your own local variables, then only functions that used technically 
'illegal' names would be affected.

Paul




More information about the Python-list mailing list