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? 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/ >
_______________________________________________ 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/SP4QCMULYBR65EW7ECU4PQTHTQASQZPP/ Code of Conduct: http://python.org/psf/codeofconduct/