[Python-ideas] Consider adding an iterable option to dataclass

Steven D'Aprano steve at pearwood.info
Fri Aug 10 20:29:41 EDT 2018


On Fri, Aug 10, 2018 at 04:01:59PM -0700, Neil Girdhar wrote:
> It would be nice if dataclasses 
> (https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass) 
> had an option to make them a sequence.

Do you have a use-case or reason for this other than "it would be nice"? 
Nice in what way? We already have namedtuple, and for backwards 
compatibility if no other reason it won't be going away. What benefit do 
we get from allowing dataclasses to do what namedtuple already does?

You already mentioned one disadvantage: namedtuple is much more memory 
efficient. What corresponding benefit do you see?

Dataclass already supports explicit conversion to tuples and dicts. What 
use-cases for sequence-ness don't they support?

Conceptually, I think of dataclasses as a record or a struct, not as a 
sequence. (I'll admit that I think of namedtuples the same way, and 
almost never make use of their tuple-ness.) I would find it strange for 
dataclass to support a sequence API out of the box.


-- 
Steve


More information about the Python-ideas mailing list