[Python-Dev] Definining properties - a use case for class decorators?
Paul Moore
p.f.moore at gmail.com
Wed Oct 19 11:42:25 CEST 2005
On 10/19/05, Michele Simionato <michele.simionato at gmail.com> wrote:
> On 10/18/05, Guido van Rossum <guido at python.org> wrote:
> > I wonder if at some point in the future Python will have to develop a
> > macro syntax so that you can write
> >
> > Property foo:
> > def get(self): return self._foo
> > ...etc...
>
> This reminds me of an idea I have kept in my drawer for a couple of years or so.
> Here is my proposition: we could have the statement syntax
>
> <callable> <name> <tuple>:
> <definitions>
>
> to be syntactic sugar for
>
> <name> = <callable>(<name>, <tuple>, <dict-of-definitions>)
Cor. That looks like very neat/scary stuff. I'm not sure if I feel
that that is a good thing or a bad thing :-)
One question - in the expansion, "name" is used on both sides of the
assignment. Consider
something name():
<definitions>
This expands to
name = something(name, (), <dict>)
What should happen if name wasn't defined before? A literal
translation will result in a NameError. Maybe an expansion
name = something('name', (), <dict>)
would be better (ie, the callable gets the *name* of the target as an
argument, rather than the old value).
Also, the <definitions> bit needs some clarification. I'm guessing
that it would be a suite, executed in a new, empty namespace, and the
<dict-of-definitions> is the resulting modified namespace (with
__builtins__ removed?)
In other words, take <definitions>, and do
d = {}
exec <definitions> in d
del d['__builtins__']
then <dict-of-definitions> is the resulting value of d.
Interesting idea...
Paul.
More information about the Python-Dev
mailing list