(original) (raw)
On Sat, Feb 14, 2015 at 2:36 PM, Georg Brandl <g.brandl@gmx.net> wrote:
> In the case of int, there is a good reason for this behavior - bool. In python,
\> we want True + True == 2\. In numpy, where binary operations preserve
\> subclasses, you have
\>
\>>>> import numpy
\>>>> numpy.bool\_(1) + numpy.bool\_(1)
\> True
I don't think numpy.bool\_ subclasses some class like numpy.int\_.
And numpy.bool\_ subclasses don't preserve type in addition:
>>> import numpy
>>> class Bool(numpy.bool\_):
... pass
...
>>> numpy.bool\_.mro()
\[, , \]
>>> Bool(1) + Bool(1)
True
>>> type(\_)
So there goes my theory. :-)
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.
Apparently numpy has it both ways. One way for scalars (see above) and the other for arrays:
>>> class Array(numpy.ndarray):
... pass
...
>>> a = Array(1)
>>> a\[0\] = 1
>>> a+a
Array(\[ 2.\])