5 ms·
From a users perspective data classes look kind of like a C struct and in particular include type annotations so fit well with type checkers. They also allow fo
by jeeeb 5y ago
From a users perspective data classes look kind of like a C struct and in particular include type annotations so fit well with type checkers. They also allow for default values and give more control over generating equality, hash, string and initialisation methods.
Comparatively named tuples are an older language feature which essentially allow you to define named accessors for tuple elements. IIRC, these days you can also define type annotations for them.
Their use case essentially overlap. Personally I much prefer data classes.
- progval 5y agoFWIW, you can also add type annotations for namedtuples, with a syntax similar to data classes: https://docs.python.org/3/library/typing.html#typing.NamedTuple https://docs.python.org/3/library/typing.html#typing.NamedTu... You can even type dictionaries this way: https://docs.python.org/3/library/typing.html#typing.TypedDict https://docs.python.org/3/library/typing.html#typing.TypedDi...
- nsonha 5y agoThis is my confusion learning python, just pick a thing for struct will ya :)