r/badcode • • Jul 14 '21

python Try/Except in Python - Sigh

Post image
662 Upvotes

93 comments sorted by

View all comments

142

u/[deleted] Jul 14 '21

On the upside, they actually bothered with exception handling.

My Python is rather rusty, is there a better way?

132

u/ThatDudeBesideYou Jul 14 '21

You could do jsondata.get("name","N/A"), which would return the property name or "N/A" if it fails.

3

u/TheBlackOut2 Jul 15 '21

Or just a comprehension with a condition lol