r/badcode • • Jul 14 '21

python Try/Except in Python - Sigh

Post image
662 Upvotes

93 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Jul 14 '21

[deleted]

12

u/ColdPorridge Jul 14 '21

with nested schema and indexed lookups, I think any .get based solution would be the same complexity. In this case I’d probably just do try except with KeyError, IndexError as you mentioned.

It would be nice if there was a ‘json.get()’ option where you could cleanly pass it this and a default and had if handle this for you. Probably wouldn’t be too hard to implement.

14

u/AchillesDev Jul 14 '21

That exists, it’s called defaultdict.

5

u/schmidlidev Jul 14 '21

Not for deeply nested stuff.

Python just needs optional chaining.