[Python-Dev] New syntax for 'dynamic' attribute access

Neil Toronto ntoronto at cs.byu.edu
Mon Feb 12 03:17:22 CET 2007


Ben North wrote:
> Hi,
>
> A few days ago I posted to python-ideas with a suggestion for some new
> Python syntax, which would allow easier access to object attributes
> where the attribute name is known only at run-time. For example:
>
> setattr(self, method_name, getattr(self.metadata, method_name))
>
> from Lib/distutils/dist.py could be rewritten
>
> self.(method_name) = self.metadata.(method_name)

I like it.

> The new syntax would also be usable in augmented assignments, as in
>
> obj.(attr_name) += 1

Even nicer; much, much better than the current spelling.

> *snip*
>
> * The draft currently allows a two-argument form, to supply a default
> value if the object has no attribute of that name. This mimics the
> behaviour of the three-argument form of getattr, but looks a bit wrong:
>
> s = obj.(attr_name, 'default string')
>
> I agree that it looks odd, but perhaps the extra expressive power
> gained might be worth the oddness.

It's not just odd, but because you can't use the result of that syntax 
as an assignment target (according to the PEP), it smells special-casey.

Neil



More information about the Python-Dev mailing list