3 ms·
$ python3 Python 3.11.6 (main, Oct 2 2023, 20:46:14) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin Type "help", "copyright", "credits" or "license" f
by wodenokoto 3y ago
$ python3
Python 3.11.6 (main, Oct 2 2023, 20:46:14) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import List
>>> def safe_navigation(obj, path: List[str], default=None):
... try:
... for key in path:
... obj = obj[key]
... return obj
... except KeyError:
... return default
...
>>> obj = {1: {1: {1: 2}}}
>>> safe_navigation(obj, [1,1,1])
2
>>> obj
{1: {1: {1: 2}}}