On Wed, Sep 1, 2021 at 12:45 PM Kevin Mills <kevin.mills...@gmail.com>
wrote:

> d = {1: {2: {3: 4}}}
> keys= 1,2,3
> print(d[*keys])  # Meaning: d[1][2][3]
> d[*keys] = None
> print(d)


This is a bad idea for a couple reasons.

One reason MRAB points to.  The `*keys` syntax is more-or-less equivalent
to "substitute a tuple" in other Python contexts; you are proposing to give
it a completely different meaning.  This would be confusing and
inconsistent.

However, let's bracket the syntax for a moment.  More important is that
"nested" data comes in shapes other than strictly nested dictionaries.
This seems to be most common in dealing with JSON data, but in concept it
can arise elsewhere.  A variety of libraries address this, with some small
differences among them.  Many are inspired by the semi-convention of "JSON
Path" that is inspired by XPath for XML.

For example, what if our data looks like this:

data = {1: [{2: {3, 4}}, {5: {6,7}}, [8, 9, 0]]}

It's not unreasonable to want to query that, and not uncommon to encounter
similar things in the JSON world (I added a deliberately ugly inconsistency
in the list of values associate with the key `1`; this is painful, but
common, in the JSON world).

A few libraries I find that try to handle this are:

https://pypi.org/project/jmespath/
https://pypi.org/project/jsonpath-ng/
https://pypi.org/project/path-dict/

There are differences in their approaches, but crucially, none want or need
changes to Python syntax.


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/7H23QVAEUF5XTMGK2JLHJVK2ESS6I5GR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to