Hacker News new | ask | show | jobs
by NeutralForest 403 days ago
My style has changed over time and part of it is thanks to static type checking in Python. I rarely use dictionaries anymore when what I actually want is a different type that functions will handle down the line. So to transfer data, I usually make frozen dataclasses where I used to use dictionaries. It's more work when you want to add fields on the fly ofc but it pays dividend anytime the logic becomes more complex.
1 comments

Agreed -- dataclasses over dicts. And for legacy code I try to move them to typed dictionaries.

Pydantic is also helpful to enforce types on json.

I've also stopped passing around argparse namespaces. I immediately push the argparse namespace into a pydantic class (although a dataclass could also be used.)