News for hackers. Stuff that matters.
Re:Attrs – the Python library everyone needs (2016) by nsonha on Friday December 24, 2021 @04:04PM (#29677721)
Not a python guy, so confused as to why a thing called namedtuple behaves like dataclasses, what are their different usecases?
Re:Attrs – the Python library everyone needs (2016) by aix1 on Friday December 24, 2021 @04:16PM (#29677802)
Named tuples have been around for a long time (since 2.6), whereas dataclasses are a relatively recent addition to the standard library (3.7).

Their differences are highlighted in the dataclasses PEP: https://www.python.org/dev/peps/pep-0557/#why-not-just-use-n...

Re:Attrs – the Python library everyone needs (2016) by nsonha on Friday December 24, 2021 @05:31PM (#29678402)
looks like the key thing is immutability
Re:Attrs – the Python library everyone needs (2016) by aix1 on Friday December 24, 2021 @05:36PM (#29678440)
To clarify, both named tuples and dataclasses can be immutable (the former are always immutable and the latter can be made immutable with `frozen=True`).

There is no way, however, to make a named tuple mutable.

Re:Attrs – the Python library everyone needs (2016) by dreamofkoholint on Friday December 24, 2021 @09:43PM (#29679874)
Key thing for me was the ability to add type hinting
Re:Attrs – the Python library everyone needs (2016) by rguillebert on Friday December 24, 2021 @04:41PM (#29678013)
Namedtuples also behave like tuples, which is great when you want to incrementally turn tuples into classes but if you want an easy way of creating classes, it's probably not a good idea to have them behave like tuples. Plus dataclasses have more features.
Re:Attrs – the Python library everyone needs (2016) by pletnes on Friday December 24, 2021 @06:04PM (#29678652)
There’s also the fact that sometimes you need an actual tuple - in some places (C extensions like numpy, opencv, …) and a dataclass just doesn’t work.
Re:Attrs – the Python library everyone needs (2016) by jeeeb on Friday December 24, 2021 @04:51PM (#29678093)
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.

Re:Attrs – the Python library everyone needs (2016) by progval on Saturday December 25, 2021 @05:55AM (#29682187)
FWIW, you can also add type annotations for namedtuples, with a syntax similar to data classes: 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.TypedDi...

Re:Attrs – the Python library everyone needs (2016) by nsonha on Saturday December 25, 2021 @11:07AM (#29684343)
This is my confusion learning python, just pick a thing for struct will ya :)