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

Neil Girdhar mistersheik at gmail.com
Sat Aug 11 05:48:24 EDT 2018

My only motivation for this idea is so that I can forget about namedtuple.
Thinking about it again today, I withdraw my suggestion until I one day see
a need for it.

On Fri, Aug 10, 2018 at 10:14 PM Eric V. Smith <eric at trueblade.com> wrote:

> On 8/10/2018 7:01 PM, 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.  This would make
> >
> > dataclass(frozen=True, order=True, sequence=True)
> >
> > an optionally-typed version of namedtuple.  It would almost totally
> > supplant it except that namedtuples have a smaller memory footprint.
> Note that type.NamedTuple already gives you typed namedtuples.
> Admittedly the feature set is different from dataclasses, though.
> > sequence would simply inherit from collections.abc.Sequence and
> > implement the two methods __len__ and __getitme__.
> Unless I'm misunderstanding you, this falls in to the same problem as
> setting __slots__: you need to return a new class, in this case since
> you can't add inheritance after the fact. I don't think
> __isinstancecheck__ helps you here, but maybe I'm missing something (I'm
> not a big user of inheritance or ABCs).
> Not that returning a new class is impossible, it's just that I didn't
> want to do it in the first go-round with dataclasses.

That's a fair point.  I'm sure you know that your decorator could always
return a new class that inherits from both Sequence and the original
class.  As a user of dataclass, I never assumed that it wouldn't do this.

> For slots, I have a sample @add_slots() at
> https://github.com/ericvsmith/dataclasses/blob/master/dataclass_tools.py.
> Maybe we could do something similar with @add_sequence() and test it
> out? It would have to be a little more sophisticated than @add_slots(),
> since it would need to iterate over __dataclass_fields__, etc.
> I'm on vacation next week, maybe I'll play around with this.

Cool, have a great vacation.

> Eric
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
> --
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/8C9iVJsba5A/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180811/97a4941a/attachment.html>

More information about the Python-ideas mailing list