On Sun, 23 May 2021 at 19:41, Todd <[email protected]> wrote: > > The pytoolz/cytoolz project already has this: > https://toolz.readthedocs.io/en/latest/api.html#toolz.dicttoolz.get_in
It seems a project that is used by many people. I think that JSON is so much used that that function could be added to the builtin dict. > > On Sun, May 23, 2021, 11:44 Chris Angelico <[email protected]> wrote: >> >> On Mon, May 24, 2021 at 1:24 AM MRAB <[email protected]> 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 -- [email protected] >> To unsubscribe send an email to [email protected] >> https://mail.python.org/mailman3/lists/python-ideas.python.org/ >> Message archived at >> https://mail.python.org/archives/list/[email protected]/message/YM6KCRUJV2ROYV2TC44DWECFJV6TG4A6/ >> Code of Conduct: http://python.org/psf/codeofconduct/ > > _______________________________________________ > Python-ideas mailing list -- [email protected] > To unsubscribe send an email to [email protected] > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/[email protected]/message/MEQM2SZQD5KPVZHUM655DRR4EV42U63Y/ > Code of Conduct: http://python.org/psf/codeofconduct/ _______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/3VAIGV6CPLOMEY7K2Z4XWIXQ7HXBJXQ6/ Code of Conduct: http://python.org/psf/codeofconduct/
