Are decorators really that different from metaclasses...

Hallvard B Furuseth h.b.furuseth at usit.uio.no
Mon Aug 30 13:06:37 EDT 2004


Paul Morrow wrote:
> I believe that we should think of assignments to __xxx__ attributes as 
> not being part of the function's body, but instead part of its 
> declaration, just as we do with its docstring.
> 
>     def circum(diameter):
>         """This describe's foo."""
>         __author__ = 'Paul Morrow'
>         __version__ = '0.1'
>         __features__ = synchronized, memoized

I really, really don't like this idea.  Assignment in Python has quite
enough pitfalls already.  Please don't make it worse.  If it looks like
an assignment to a local variable, it should be an assignment to a local
variable.  If we are going to invent a syntax for declaring function
attributes inside the function, why confuse the issue by making it look
like it does something else?

It's true that it makes it look more like what __*__ attribute
assignment in class bodies does, but the simple fact is that class
bodies are executed when the class statement is executed, and function
bodies are not executed when the def statement is executed.  Now you
want part of the function bodies to be executed at def time, and part at
call time.

__*__ assigments in class bodies can look a little confusing if you
don't think of how class declarations work, but it's perfectly simple
once you think of how they do work: Execute the class body - no magic
involved (as far as I know).  Then pass the resulting __dict__ to the
class creation machine - and _that_ recognizes __metaclass__, __slots__
and I don't know what else and does magic with them.

Once you remember that, __*__ variables in both class bodies and
function bodies do exactly what one would expect them to do.  Stronger
magic, like grabbing the doc string and putting it in __doc__, has its
own syntax.

-- 
Hallvard



More information about the Python-list mailing list