[Python-ideas] Bring line continuation to multi-level dictionary lookup

Zachary Ware zachary.ware+pyideas at gmail.com
Thu Sep 17 06:24:03 CEST 2015


On Wed, Sep 16, 2015 at 11:07 PM, John Wong <gokoproject at gmail.com> wrote:
> So here I am, thinking, what if we can do this?
>
> response(
>     ["DescribeDBSnapshotsResponse"]
>     ["DescribeDBSnapshotsResult"]
> )
>
> You get the point. This looks kinda ugly, but it doesn't require so many
> assignment. I think this is doable, after all [ ] is still a method call
> with the key name passed in. I am not familar with grammar, so I don't know
> how hard and how much the implementation has to change to adopt this.
>
> Let me know if this is a +1 or -10000000 bad crazy idea.

I think a much better idea is to create a utility function:

   def dig(container, *path):
       obj = container
       for p in path:
           obj = obj[p]
       return obj

Then you can do your long lookup like so:

   engine = dig(response,
                'DescribeDBSnapshotsResponse',
                'DescribeDBSnapshotsResult',
                'DBSnapshots',
                0,
                'Engine')

This came up on python-list a month or two ago; perhaps there's some
merit in finding a place to stick this utility function.

-- 
Zach


More information about the Python-ideas mailing list