4 ms·
Be careful with named tuples: >>> from collections import namedtuple >>> Time = namedtuple('Time', ['hours', 'minutes']) >>> Point = namedtuple('Po
by deniska 10y ago
Be careful with named tuples:
>>> from collections import namedtuple
>>> Time = namedtuple('Time', ['hours', 'minutes'])
>>> Point = namedtuple('Point', ['x', 'y'])
>>> t = Time(hours=7, minutes=40)
>>> p = Point(x=7, y=40)
>>> p == t
True
They're still tuples. They're meant more as a tool for making an already existing tuple mess slightly more managable rather than creating a new mess.
For making plain data objects take less boilerplate to create have a look at attrs: https://attrs.readthedocs.io https://attrs.readthedocs.io
- deleted 10y ago[deleted]
- SEJeff 10y agoStrong +1 for attrs. It is a really nice library and quite easy to use.
- tedmiston 10y agoInteresting find with attrs. My main use case for namedtuples is a class as a data container which doesn't require any special methods. This might be a better alternative.
- thijsvandien 10y agoUgh, that's terrible... Thanks for pointing it out!