dict!ident as equivalent of dict["ident"]

Roy Smith roy at panix.com
Sun May 21 14:52:22 CEST 2006


In article <mailman.6027.1148213981.27775.python-list at python.org>,
 Alexander Kozlovsky <alexander.kozlovsky at gmail.com> wrote:

> Hello all!
> I have small silly syntax suggestion (SSSS)
> 
> In many cases, keys in dictionary-like objects are strings,
> and moreover - valid Python identifiers. Something like:
> 
>     foo["bar"]["baz"].x = y
> 
> How about small syntactic sugar:
> 
>     foo!bar!baz.x = y

Ugh.  Syntactic sugar like that leads to language obesity.  Anyway, you can 
already do what you want with just a little bit of work.  Define a class 
(perhaps a subclass of dict, if you like) with a __getattr__ method.  Then 
you can just do

foo.bar.baz.x = y

with no changes needed to the language.



More information about the Python-list mailing list