Data structures to represent data in Python
August 16, 2022
- https://www.youtube.com/watch?v=T-TwcmT6Rcw&t=1390s
- https://docs.python.org/3/library/dataclasses.html
- https://stackoverflow.com/questions/51671699/data-classes-vs-typing-namedtuple-primary-use-cases
- https://shayallenhill.com/python-struct-options/
- https://realpython.com/python-data-classes/
Comments from Nic:
I see it as a hierarchy of needs where I choose the "simplest" for the use case required:
tuple
- frozen listcollections.namedtuple
- tuple with labels for positions (I use this for when I want to return multiple objects that don't really go together normally, eg pytest example test case parameters)typing.NamedTuple
- namedtuple with type hintsdataclasses
- for cases where the object can be thought of as a genuine data modelpydantic
- for when you have a data model and want validation, conversion and serialisation included
Given that we use pydantic extensively already and I like validation the gap in use cases between typing.NamedTuple and pydantic is quite small in my mind which is why I don't default to using them much.