<div dir="ltr">Hi everyone.<div><br></div><div>I work with APIs which have deep nested dictionary structure response. Imagine a simplified case:</div><div><br></div><div>foo = {1: {2: {3: {4: {5: 6 } } } }</div><div><br></div><div>Now imagine I need to get to 6:</div><div><br></div><div>foo['1']['2']['3']['4']['5']['6']</div><div><br></div><div>This looks managable, but if the key name is long, then I certainly will end doing this to respect my style guide. To make it concrete, let's use something reallistic, a response call from AWS API:</div><div><br></div><div><div>response = {'DescribeDBSnapshotsResponse': {'ResponseMetadata': {'RequestId': '123456'}, 'DescribeDBSnapshotsResult': {'Marker': None, 'DBSnapshots': [{'Engine': 'postgres'}]}}}</div></div><div><br></div><div>If I had to get to the Engine I'd do this:</div><div><br></div><div>detail_response = response["DescribeDBSnapshotsResponse"]</div><div>result = detail_response["DescribeDBSnapshotsResult"]</div><div><br></div><div>This is only a few level deep, but imagine something slightly longer (I strict out so much from this response). Obviously I am picking some real example but key name being really long to sell my request.</div><div><br></div><div>Can we do it differently? How about<br></div><div><div>print(response.get(</div><div>  "DescribeDBSnapshotsResponse").get(</div><div>  "DescribeDBSnapshotsResult").get(</div><div>  "DBSnapshots")[0].get(</div><div>  "Engine"))</div></div><div><br></div><div>Okay. Not bad, almost like writing in Javascript except Python doesn't allow you to do line continuation before the got at all, so you are stuck with (.</div><div><br></div><div>But the problem with the alternative is that if DescribeDBSnapshotsResult is a non-existent key, you will just get None, because that's the beauty of the .get method for a dictionary object. So while this allows you to write in slightly different way, I am costing silent KeyError exception. I wouldn't know which key raised the exception. Whereas with [key1][key2] I know if key1 doesn't exist, the exception will explain to me that key1 does not exist.</div><div><br></div><div>So here I am, thinking, what if we can do this?</div><div><br></div><div>response(</div><div>  ["DescribeDBSnapshotsResponse"]</div><div>  ["DescribeDBSnapshotsResult"]</div><div>)</div><div><br></div><div>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.</div><div><br></div><div>Let me know if this is a +1 or -10000000 bad crazy idea.</div><div><br></div><div>Thanks.</div><div><br></div><div>John</div></div>