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'], from what I
read, this is very terse and terse is good in most cases good in my
opinion. It removes the error handling noise by replacing it by one char

I'm repeating myself here, but again, the use case is to fetch deedly
nested data. All this talk make me notice that IndexError, and KeyError are
LookupError instances but AttributeError is not, still pep 505 have an
elegant solution for all these three. Also it handles the None?.method()
case, when something return an object or None and you want to call a method
on this if is not None.

I think we should put more effort on pep 505

>
> I've also wanted this a couple of times, although I can't remember the
> reason. It's not just about traversing incomplete nested data structures. I
> don't see much disadvantage since the analogy with `dict.get` would make it
> very easy to learn and understand, to the point that I've wondered why this
> doesn't exist already.
>
> On Sat, Jun 27, 2020 at 5:09 PM Guido van Rossum <gu...@python.org> wrote:
>
>> Please read PEP 505 before rehashing this old idea.
>>
>> On Sat, Jun 27, 2020 at 06:35 Daniel. <danielhi...@gmail.com> wrote:
>>
>>> When I need to traverse nested dicts, is a common pattern to do
>>>
>>> somedict.get('foo', {}).get('bar', {})
>>>
>>> But there is no such equivalent for arrays, wouldn't be nice if we can
>>> follow
>>>
>>> somedict.get('foo', {}).get('bar', []).get(10) ... ?
>>>
>>> What I do in this case is surround with try/except IndexError and set
>>> some variable to None, or wrap the try/except block in a function. But this
>>> is noise in my opinion, I just want to follow the same reasoning that I
>>> have with dicts:  I'm searching for some path deep nested, and want a
>>> default if not found.
>>>
>>> What do you think?
>>>
>>> Regards
>>>
>>> --
>>> “If you're going to try, go all the way. Otherwise, don't even start.
>>> ..."
>>>   Charles Bukowski
>>> _______________________________________________
>>> 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/LLK3EQ3QWNDB54SEBKJ4XEV4LXP5HVJS/
>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>
>> --
>> --Guido (mobile)
>> _______________________________________________
>> 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/L64EPHNKVRFLSBJE53GPQ4JFNVNOPH7U/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>

-- 
“If you're going to try, go all the way. Otherwise, don't even start. ..."
  Charles Bukowski
_______________________________________________
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/UOJL3T5FSLDZ7HY4WGRT4LCMLWSFMNF6/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to