I often see this pattern to navigate through dictionaries in a fault-tolerant manner:
a.get('k1', {}).get('k2', {}).get('k3', '')
This is, however, not possible if the intermediary objects are class instances rather than dictionaries. Is there a terse way to do (something like) the following:
a?.method1()?.method2()?.method3()...
Expanding to:
if a is None:
return None
obj = a.method1()
if obj is None:
return None
obj = obj.method2()
if obj is None:
return None
obj = obj.method3()
return obj
I guess it might be possible with some variant of the builder pattern, but that would not be feasible if the methods above are in an API not controlled by the user.