<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Feb 12, 2015 at 4:36 PM, Ethan Furman <span dir="ltr"><<a href="mailto:ethan@stoneleaf.us" target="_blank">ethan@stoneleaf.us</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I suspect the last big hurdle to making built-in data structures nicely subclassable is the insistence of such types to<br>
return new instances as the base class instead of the derived class.<br>
<br>
In case that wasn't clear ;)<br>
<br>
--> class MyInt(int):<br>
... def __repr__(self):<br>
... return 'MyInt(%d)' % self<br>
...<br>
--> m = MyInt(42)<br>
--> m<br>
MyInt(42)<br>
--> m + 1<br>
43<br>
--> type(m+1)<br>
<class 'int'><br>
<br>
Besides the work it would take to rectify this, I imagine the biggest hurdle would be the performance hit in always<br>
looking up the type of self. Has anyone done any preliminary benchmarking? Are there other concerns?<br></blockquote></div><br></div><div class="gmail_extra">Actually, the problem is that the base class (e.g. int) doesn't know how to construct an instance of the subclass -- there is no reason (in general) why the signature of a subclass constructor should match the base class constructor, and it often doesn't.<br clear="all"></div><div class="gmail_extra"><br></div><div class="gmail_extra">So this is pretty much a no-go. It's not unique to Python -- it's a basic issue with OO.<br><br></div><div class="gmail_extra">-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)</div>
</div></div>