[Python-Dev] Dict access with double-dot (syntactic sugar)

Brian Curtin brian.curtin at gmail.com
Thu Mar 24 15:16:38 CET 2011


On Thu, Mar 24, 2011 at 06:40, Jameson Quinn <jameson.quinn at gmail.com>wrote:

> "class attrdict" is a perennial dead-end for intermediate pythonistas who
> want to save 3 characters/5 keystrokes for item access. Other languages such
> as javascript allow "somedict.foo" to mean the same as "somedict['foo']", so
> why not python? Well, there are a number of reasons why not, beginning with
> all the magic method names in python.
>
> But saving keystrokes is still a reasonable goal.
>

Code is read far more often than it is written, so readability tends to
count more than most other metrics.

So what about a compromise? Allow "somedict..foo", with two dots, to take
> that place. It still saves 2 characters (often 4 keystrokes; and I find even
> ', "[", or "]" harder to type than ".").
>

I don't see the benefit, but maybe it'll save a few bytes in file size.
Anyone reviewing your code now has to think "does this need one or two
dots?"

Anyways, why not just do something like this:

class AttrDict(dict):
    def __getattr__(self, attr):
        return super(AttrDict, self).__getitem__(attr)

>>> d = AttrDict()
>>> d["a"] = 1
>>> d.a
1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20110324/56f044d7/attachment.html>


More information about the Python-Dev mailing list