3 ms·
Data classes can gracefully replace tuples everywhere. Set frozen, then use a mixin or just author a getitem and iter magic, and you’re done.
by doctorpangloss 2y ago
Data classes can gracefully replace tuples everywhere. Set frozen, then use a mixin or just author a getitem and iter magic, and you’re done.
- heavyset_go 2y agoThey can't if you're using tuple unpacking.
- doctorpangloss 2y agodef __iter__(): yield self.my_field yield self.my_other_field Recreates tuple unpacking.
- Joker_vD 2y agoJust mix this class in: class IWantToHaveNamedTupleInterfaceButWasToldTheyAreBad: def __iter__(self): return iter(self.__dict__.values()) and voila: @dataclass(frozen=True) class Point(IWantToHaveNamedTupleInterfaceButWasToldTheyAreBad): x: int y: int p = Point(1, 2) x, y = p
- heavyset_go 2y agoYes, but that is insane.