Are decorators really that different from metaclasses...

Jeff Epler jepler at unpythonic.net
Thu Sep 2 09:46:41 EDT 2004


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)*)

As far as thinking of "non-contrived" examples, the only code I've ever
written which explicitly used function attributes used non-underscored
names[1], so your syntax wouldn't help me.  You could use __* (class
private; but I wouldn't have wanted name-mangling) or _* (private) but
now you've reached areas where you clearly can't claim you'll affect no
existing code.

On the other hand, looking at examples where the names have no meaning
seems relevant to me, because it shows just how much complication you've
added, and how much currently legal code would have its meaning changed
under your proposal.

> Rewrite and you'll see...
> 
>      __author__ = 'Morrow'      # defines the container's author
>      def m():
>          __author__ = 'Smith'   # defines m's author
>          __doc__ = __author__   # defines m's docstring
>      print m.__doc__

It sounds like you're suggesting m.__doc__ will be 'Smith'.  Now I have to be confused that
    y = 3
    def n():
        __x__ = y
is the same as
    def m(): pass
    m.__x__ = y
but
    y = 3
    def n2():
        __x__ = __y__
is the same as
    m.__x__ = m.__y__
and will raise NameError when the body of the module containing n2
is executed.

But, oh well, you would "never write that", so it doesn't matter that
you can't give it sane semantics.

Jeff
[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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040902/d0f656c6/attachment.sig>


More information about the Python-list mailing list