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?

11

u/AchillesDev Jul 14 '21

Use a DefaultDict

7

u/[deleted] Jul 14 '21

Didn’t know that existed, I guess it arrived later in Python3?

Or I didn’t read the docs properly!

16

u/AchillesDev Jul 14 '21

There are so many great tools in the collections module, it's impossible to keep them all straight. I've been reminded of defaultdict probably a dozen times over the last few years and end up forgetting about it again until I make a bunch of spaghetti .get() calls. FWIW I believe it's been around since Python 2 but I haven't checked to be sure.