Are decorators really that different from metaclasses...

Jess Austin jess.austin at gmail.com
Sat Aug 28 08:49:05 EDT 2004


Paul Morrow <pm_mon at yahoo.com> wrote in message news:<mailman.2550.1093655878.5135.python-list at python.org>...
> Jess Austin wrote:
> 
> > So you have:
> > 
> > def foo(x):
> >     __meta_variable__ = 42
> >     variable = 5
> >     return dir()
> > 
> > And you're volunteering to explain to a newbie, or even a nearly
> > intermediate programmer like myself, why the list that is returned
> > from this function contains 'x' and 'variable' but not
> > '__meta_variable__'?  
> 
> Sure.  Magic attributes defined at the top level of a function (e.g. 
> your __meta_variable__) aren't intended to be local variables.  You'll 
> see that as you start looking at more experienced programmer's code. 
> Therfore, they shouldn't be treated as local variables.  For example, 
> suppose we changed your function a little...
> 
>    def foo(x):
>       __author__ = 'Jess Austin'
>       __version__ = '1.0'
>       variable = 5
>       return dir()
> 
> See the difference?

I see that some of these variables have underscores and some don't. 
Why didn't you respond to my detailed explanation of how binding and
scoping work in Python?  Would it be worth my time to restate the
explanation in simpler terms?


> >>Others seem to want to specify a function's metadata outside of the 
> >>function def, which just doesn't seem pythonic (IMO).
> > 
> > 
> > If you really understood Python's scoping rules, you would know that
> > this opinion is the opposite of the truth.
> 
> A decorator is a kind of meta information.  So are docstrings.  The 
> proposals I've been seeing (A1 and J2 in particular) declare decorators 
> and docstrings outside of the function def (not inside of the function def).

I'll be more specific.  "Others seem to want to specify a function's
metadata outside of the function def": truth.  "which just doesn't
seem pythonic": opposite of truth.  "(IMO)": apparently truth. 
Pythonic is a word with a meaning (in this newsgroup anyway), and
modifying an object's namespace from outside _is_ pythonic: we've
always been able to do so (at least since mid-1999 when I started
using python) and we always will be able to do so.  Your proposed
arbitrary character-driven change to Python's conventional scoping
rules would _not_ be pythonic.  It may in fact be your opinion that
such a change would be pythonic, but I correctly classified that
opinion as the opposite of truth.

 
> > Of all the current
> > incarnations of function "metadata", only one may be set within the
> > function, and that one is not set using a standard binding statement:
> > 
> 
> True and True, provided that you believe that docstrings are the only 
> kind of function metadata.  I believe though that attributes like 
> __author__ and __version__ above are also function metadata.

Metadata is a programmer's convention, or perhaps in this case
religious belief.  I'm not certain what your precise convention is
since you haven't explained it.  You evidently expect all
"experienced" programmers to know it and respect it.  Apparently it
has something to do with underscore characters.  Python accomodates
this particular convention, but it will never enforce it on the rest
of us, and that is a GOOD THING.


> > In broader commentary, as I've said, pep318
> > isn't really about metadata.  One reason is that metadata isn't that
> > important.  
> 
> By this you mean "in your opinion", right?

Of course.  The interesting thing about my opinions is that I've
provided reasonable prima facie justification for them.  Thoughtful
interlocutors would respond to that justification.

yours,
Jess



More information about the Python-list mailing list