On Thu, Jul 2, 2020 at 9:33 PM Daniel. <danielhi...@gmail.com> wrote:

> Em ter., 30 de jun. de 2020 às 15:49, Alex Hall <alex.moj...@gmail.com>
> escreveu:
>
>> I think I'm missing something. Daniel wants a `list.get` method with
>> similar semantics to `dict.get`. So instead of writing:
>>
>> ```
>> try:
>>     x = lst[i]
>> except IndexError:
>>     x = default
>> ```
>>
>> one could write `x = lst.get(i, default)`. How would you rewrite that
>> with PEP 505?
>>
> With PEP 505 is not possible to have an arbitrary default as the result of
> null coalesced expression will be None if a None is found. The lst.get(i)
> would be rewritten as lst?[i], and composing dct.get('foo', []).get(0,
> []).get('bar') would be something like dct?['foo']?[0]?['bar']
>

No, that's not right. `dct?['foo']` translates to `dct['foo'] if dct is not
None else None`, which is very different from `dct.get('foo')`. Similarly
`lst?[i]` is for when `lst` might be None, not empty. All PEP 505 gives you
in the case where keys might be missing or lists might be empty is that you
can write `.get` without defaults, e.g.
`dct.get('foo')?.get(0)?.get('bar')`.

I would quite like to have PEP 505, but I don't know how to revive it. And
I'm not surprised that it's deferred, there's generally reluctance to add
new syntax, especially symbols.

On the other hand, `list.get` seems very doable to me. It's not new syntax.
It would be extremely easy to learn for anyone familiar with `dict.get`,
which is pretty much essential knowledge. You'd probably have some people
guessing it exists and using it correctly without even seeing it first in
other code or documentation. I haven't seen anyone in this thread
suggesting any cost or downside of adding the method, just people asking if
it would be useful. I feel like I answered that question pretty thoroughly,
then the thread went quiet.
_______________________________________________
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/L6M7AKQ7L6BBU5YRKAET6HPQFFDFR7TS/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to