On Tue, Sep 1, 2020 at 9:59 AM David Mertz <mertz@gnosis.cx> wrote:
On Tue, Sep 1, 2020 at 11:45 AM Steven D'Aprano <steve@pearwood.info> wrote:
(8) Dict unpacking is permitted:

    items = {'spam': 1, 'eggs': 2}
    obj[index, **items]
    # equivalent to obj[index, spam=1, eggs=2]

I would prefer to disallow this, at least initially.  None of the use cases I've seen have an actual need for dict unpacking, and it generally just seems to follow from the analogy with function calls.  I think not allowing that encourages use of index-related operations rather than "another spelling of `.__call__()`.

I think we need this for the same reason why we need **kwargs in normal function calls: it makes it possible to forward dictionaries of arguments to another method.

In particular, indexing methods with a signature like __getitem__(self, value=None, /, **kwargs) will be useful for indexing on "labeled arrays", where dimension names are determined dynamically. Then assuredly someone (e.g., in library code) is going to want to build up these kwargs with dynamic keys.

If we don't have dict unpacking, you'd have to write obj.__getitem__(**kwargs) rather than obj[**kwargs], which is much more verbose and goes against the principle that you shouldn't need to call double-underscore methods in normal Python code. It's also more error prone, particularly in the mixed positional/keyword argument case, because you have to reverse engineer Python's syntax to figure out tuple packing.

The dead increasingly dominate and strangle both the living and the
not-yet born.  Vampiric capital and undead corporate persons abuse
the lives and control the thoughts of homo faber. Ideas, once born,
become abortifacients against new conceptions.
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/DZLDC5ET5UUGFBOXJ534L4NWFHPQQDG6/
Code of Conduct: http://python.org/psf/codeofconduct/