I have code that goes roughly like this:
def foo(keys):
a = create_a()
b = None
c = None
args = []
for key in keys:
if key in a:
arg = a[key]
else:
b = b or create_b()
if key in b:
arg = b[key]
else:
c = c or create_c()
if key in c:
arg = c[key]
else:
raise RuntimeError(f"Key {key} not found anywhere")
args.append(arg)
return some_func(args)
I'm looking for a bunch of keys within objects a, b and c. b and c are created lazily only when a key wasn't found in a.
My real code also has a for loop in each step so the nesting is getting out of hand.
Is it possible to do this without the else nesting? I don't think "easier to ask for forgiveness" can help with this at all, but I'm open to being proven wrong.
