[Python-Dev] Extended Function syntax
John Williams
jrw@pobox.com
Thu, 23 Jan 2003 09:28:19 -0600
David Goodger wrote:
> John Williams sent a very rough candidate PEP in October that was
> interesting (below). I sent back some suggestions (below the PEP), but I
> haven't received anything back yet. Perhaps a joint PEP with syntax
> alternatives?
Thanks for bringing this up. I've been following python-dev but I've
mostly gotten sidetracked from Python stuff since then. I'm also having
second thoughts about the whole idea of my proposal, since I basically
wrote it in a fit of excitement over possibilities of metaclasses.
Compared to the other proposal going around (which I'll call Guido's,
since he brought it up), the really big advantage of my proposal is that
you can use it to do something like adding a property to a class
implicitly by defining its getter and setter methods:
class A(object):
def get foo(self):
"Getter for property 'foo'."
return self.__foo
def set foo(self, foo):
"Setter for property 'foo'."
self.__foo = foo
It's critical here that neither of these declarations actually defines
the name "foo"; they define names like "get foo" and "set foo", and it's
up to the metaclass to expose these methods in a sane way (i.e. by
creating a property named "foo" in this case).
I agree with the criticisms that others have made about this proposal,
but the real killer (for me, at least) is that using method name
modifiers would require case-by-case support in the metaclass to achieve
the desired effects. Guido's proposal (adding expressions after the
argument list) is easy to extend and generally much less magical, since
the modifier expressions don't have to have their meaning "interpreted"
by a metaclass.
At this stage I'd much rather see Guido's proposal implemented, unless
someone comes up with a truly ingenious way to combine the advantages of
both.