Are decorators really that different from metaclasses...
Jeff Shannon
jeff at ccvcorp.com
Wed Sep 1 21:15:17 EDT 2004
Paul Morrow wrote:
> Hallvard B Furuseth wrote:
>
>> Yes, I can compare that. It's fine in a class body, which is executed
>> when the class definition is executed. When I see it in a function
>> definition, I see a strange use of the assignment operator, which is
>> normally a 'run-time' operator, in a place where it not being executed
>> at run-time.
>>
>
> It's also a 'definition-time' operator, as it often appears in class and
> module definitions. So why not be consistent and let it appear in
> function definitions too? What is so outrageous about this idea? On the
> 'strange-meter', does it rank as high as docstrings or meaningful
> indentation?
Yes, it does, because of the way that definition works in Python. Keep
in mind that definitions are generated at *run-time*, with dependent
suites being handled appropriately according to the type (class, def) of
compound statement being executed. Assignment isn't working any
differently in class definitions than it is anywhere else; it's just
that suites are handled slightly differently in class statements than
they are in def statements. (And this different behavior is essential,
as that's what creates the difference between a class and a function.)
You're asking now that what appears to be a single suite is actually two
separate entities, even though there's no visual indication of the
break-point (except for the presence of "magic" variables). One of
those suites is executed when the def statement is; the other is
executed when the function object is called. Wildly different semantics
from very similar-looking code at the same level of indentation, based
on what *looks* to anyone familiar with current Python as if it's merely
an odd naming convention.
(I'm sure that you'll argue that the presence of those "magic" variables
is enough distinction between the two radically-different blocks of
code; I'm also pretty confident that very few people will agree with
you, and that nobody with a reasonable understanding of Python internals
will be among those few people.)
Jeff Shannon
Technician/Programmer
Credit International
More information about the Python-list
mailing list