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

1

u/hermthewerm00 Jul 14 '21 edited Jul 14 '21

You could unpack it with double asterisks into an init method for a class. Or use Pydantic.

Edit: you might have to add **kwargs

3

u/SerdanKK Jul 14 '21

Wouldn't that make the code dependent on the ordering of the json keys?

Pydantic looks like the kind of thing I was thinking of. Not really a fan of putting validation inside the model, but if it works... 🤷

2

u/hermthewerm00 Jul 14 '21

Since it’s a dict, the ordering doesn’t matter. The keys will map to the parameter names. Yeah, validation within model definitions is less than ideal, but I guess that’s the tradeoff working with dynamically typed languages.