<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Feb 14, 2015 at 2:36 PM, Georg Brandl <span dir="ltr"><<a href="mailto:g.brandl@gmx.net" target="_blank">g.brandl@gmx.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="">> In the case of int, there is a good reason for this behavior - bool. In python,<br>
> we want True + True == 2. In numpy, where binary operations preserve<br>
> subclasses, you have<br>
><br>
>>>> import numpy<br>
>>>> numpy.bool_(1) + numpy.bool_(1)<br>
> True<br>
<br>
</span>I don't think numpy.bool_ subclasses some class like numpy.int_.</blockquote></div><br>And numpy.bool_ subclasses don't preserve type in addition:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra">>>> import numpy</div><div class="gmail_extra">>>> class Bool(numpy.bool_):</div><div class="gmail_extra">... pass</div><div class="gmail_extra">...</div><div class="gmail_extra">>>> numpy.bool_.mro()</div><div class="gmail_extra">[<class 'numpy.bool_'>, <class 'numpy.generic'>, <class 'object'>]</div><div class="gmail_extra">>>> Bool(1) + Bool(1)</div><div class="gmail_extra">True</div><div class="gmail_extra">>>> type(_)</div><div class="gmail_extra"><class 'numpy.bool_'></div><div class="gmail_extra"><br></div><div class="gmail_extra">So there goes my theory. :-)</div><div class="gmail_extra"><br></div><div class="gmail_extra">I think all these examples just highlight the need for a clear guidance when self.__class__() can be called in base classes to construct instances of derived classes.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Apparently numpy has it both ways. One way for scalars (see above) and the other for arrays:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra">>>> class Array(numpy.ndarray):</div><div class="gmail_extra">... pass</div><div class="gmail_extra">...</div><div class="gmail_extra">>>> a = Array(1)</div><div class="gmail_extra">>>> a[0] = 1</div><div class="gmail_extra">>>> a+a</div><div class="gmail_extra">Array([ 2.])</div></div><div class="gmail_extra"><br></div></div></div>