<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 11 August 2018 at 01:29, Eric V. Smith <span dir="ltr"><<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-">On 8/10/2018 7:01 PM, Neil Girdhar wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">[...]</blockquote></span><span class="gmail-">[...]<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
sequence would simply inherit from collections.abc.Sequence and implement the two methods __len__ and __getitme__.<br>
</blockquote>
<br></span>
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).<br>
<br></blockquote><div><br></div><div>Here are three points to add:</div><div><br></div><div>1. collections.abc.Sequence doesn't have a __subclasshook__, i.e. it doesn't support structural behaviour. There was an idea as a part of PEP 544 to make Sequence and Mapping structural, but it was rejected after all.</div><div>2. Mutating __bases__ doesn't require creating a new class. So one can just add Sequence after creation. That said, I don't like this idea, `typing` used to do some manipulations with bases, and it caused several confusions and subtle bugs, until it was "standardised" in PEP 560.</div><div>3. In my experience with some real life code the most used tuple API in named tuples is unpacking, for example:</div><div><br></div><div>    class Row(NamedTuple):</div><div>        id: int</div><div>        name: str</div><div><br></div><div>    rows: List[Row]</div><div><br></div><div>    for id, name in rows:</div><div>        ...</div><div><br></div><div>I proposed to add it some time ago in <a href="https://github.com/ericvsmith/dataclasses/issues/21">https://github.com/ericvsmith/dataclasses/issues/21</a>, it will be enough to just generate an __iter__ (btw such classes will be automatically subclasses of collections.abc.Iterable, which is structural):</div><div><br></div><div><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:11.9px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-radius:3px;word-break:normal;color:rgb(36,41,46);text-decoration-style:initial;text-decoration-color:initial"><span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">@data</span>(<span class="gmail-pl-v" style="box-sizing:border-box;color:rgb(227,98,9)">iterable</span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span><span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">True</span>)
<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">class</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">Point</span>:
    x: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">int</span>
    y: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">int</span>
origin <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> Point(<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">0</span>, <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">0</span>)
x, y <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> origin</pre><br></div><div>But this idea was postponed/deferred. Maybe we can reconsider it?</div><div><br></div><div>--</div><div>Ivan</div><div><br></div><div><br></div></div></div></div>