Are decorators really that different from metaclasses...
Steven Bethard
steven.bethard at gmail.com
Tue Aug 24 18:55:09 EDT 2004
Paul Morrow <pm_mon <at> yahoo.com> writes:
>
> ...that they warrant an entirely new syntax?
[snip]
> So why don't they share a similar syntax?
>
> class Foo:
> """ This is the docstring for class Foo. """
> __metaclass__ = M
> # body of class goes here
>
>
> def baz():
> """ This is the docstring for function baz. """
> __features__ = synchronized, memoized
> # body of function goes here.
As I'm sure someone has already mentioned, this has been brought up before.
I'd especially look at:
http://mail.python.org/pipermail/python-dev/2004-August/048176.html
That being said, I think the main objection to this syntax is the same
objection to any inside-def syntax: The code inside a funcdef block (with the
exception of docstrings, which Guido has repeatedly said he regrets putting
there) is executed as part of the /execution/ of the function, not
the /definition/.
Note a classdef is not really the same in this respect. The code inside a
classdef block is executed as part of the /definition/ of the class. Compare
what happens when you call Foo() with what happens when you call baz(). With
the call to Foo(), you don't execute the code in the classdef block; you
execute the code in __new__ and/or __init__. With the call to baz(), you /do/
execute the code in the funcdef block.
So the basic answer is that decorators are different from metaclasses because
the code inside the funcdef block is executed when the function is /called/,
while the code inside the classdef is executed when the class is /declared/.
It may be a subtle difference, but I believe this is what Guido disliked so
much about the inside-def syntaxes...
Steve
More information about the Python-list
mailing list