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

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Mar 24 21:38:53 CET 2011


Jameson Quinn 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?

I think the main reason this is a dead end is these
intermediate pythonistas eventually come to realise that,
if you program pythonically, it's actually extremely
rare that you need to index a dictionary with a constant.

Either you have a mostly-fixed set of field names, in
which case you should be using a custom class instead of
a dict, or the set of keys is dynamic, in which case
you're mostly indexing with computed values. Lots of
somedict['foo'] appearing is a code smell.

So there wouldn't be enough use for a somedict..foo syntax
to justify its existence.

-- 
Greg


More information about the Python-Dev mailing list