The pytoolz/cytoolz project already has this:
https://toolz.readthedocs.io/en/latest/api.html#toolz.dicttoolz.get_in

On Sun, May 23, 2021, 11:44 Chris Angelico <ros...@gmail.com> wrote:

> On Mon, May 24, 2021 at 1:24 AM MRAB <pyt...@mrabarnett.plus.com> wrote:
> > Also, if the first lookup returns a list or a tuple, and an argument can
> > be an index of that list, would be make sense to add a similar method to
> > lists and tuples?
>
> Or, better: make it a stand-alone function, not a method of anything.
> Although that kinda takes it out of python-ideas territory because it
> could easily be a personal library function instead:
>
> _sentinel = object() # or see other proposals
> def get_deep(obj, *keys, default=_sentinel):
>     if len(keys) == 1: keys = list(keys[0])
>     try:
>         for key in keys:
>             obj = obj[key]
>     except (LookupError, TypeError): # the OP did include TypeError
>         if default is not _sentinel: return default
>         raise
>     return obj
>
> Done. Personally, I'd just go with "except LookupError:", but this is
> another advantage of personal library functions: you don't have to
> bikeshed them with everyone else :)
>
> ChrisA
> _______________________________________________
> 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/YM6KCRUJV2ROYV2TC44DWECFJV6TG4A6/
> 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/MEQM2SZQD5KPVZHUM655DRR4EV42U63Y/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to