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

Brian Curtin brian.curtin at gmail.com
Thu Mar 24 17:15:46 CET 2011


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

> Consider:
>
> def fun1(argument):
>     print argument1
>
> fun1(argument="spam")
>
> def fun2(**kw):
>     print kw["argument"]
>
> Why should I need quotes around "argument" in just one of those places?
> What if I left them off, and there happened to be a global variable named
> "argument"? Why shouldn't I be able to say:
>
> def fun2(**kw):
>     print kw..argument
>
> (in real life, there would be a try... except block in case there was no
> argument, I'm just showing the simplest case here.)
>

I can certainly see use cases for this, but none generic enough for the
standard library.

Let's take the configparser module for example: in 3.2 ConfigParser objects
can now be accessed like dictionaries. Looking at the examples in the
documentation [0], an immediate problem comes to mind:

    print(config..bitbucket.org..user)
    *boom*

If you're going to be doing attribute access, it's likely that you know the
name of the attribute -- you wrote the code knowing what to expect in the
first place. If you know the names of the attributes you're going to be
dealing with, why not just store them in a class, or even a class with
__slots__ defined for each attribute?

The double-dot notation will only work when you already know the key. When
iterating over keys, you're going to resort back to dict[key] or
getattr(dict, key) to get the value.

In the end, it's syntactic sugar for a limited set of applicable cases.



[0] http://docs.python.org/release/3.2/library/configparser
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20110324/7f724df3/attachment.html>


More information about the Python-Dev mailing list