Skip to content Skip to sidebar Skip to footer

Confusing Type Of Results Of Binary Arithmetic Operations Involving Python Builtin Numeric Types And Numpy Types Float64 And Complex128

Using multiplication operator mul for example, code import numpy as np v = 1.0 a = float(v) b = np.float64(v) print(type(a*b), type(a.__mul__(b))) a = complex(v) b = np.complex

Solution 1:

I may be wrong about the priority. There is a priority value that controls how ndarray and its subclasses interact, but with the Python numbers it may be just a matter of NotImplemented, or subclassing

https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types

Note If the right operand’s type is a subclass of the left operand’s type 
and that subclass provides a different implementation of the reflected 
method for the operation, this method will be called before the left 
operand’s non-reflected method. This behavior allows subclasses to 
override their ancestors’ operations.

With your example:

In [107]: a = 1.0; b = np.float64(1.0)
In [108]: type(a*b)
Out[108]: numpy.float64
In [109]: type(a.__mul__(b))
Out[109]: float
In [110]: type(b.__rmul__(a))
Out[110]: numpy.float64
In [111]: type(b).__mro__
Out[111]: 
(numpy.float64,
 numpy.floating,
 numpy.inexact,
 numpy.number,
 numpy.generic,
 float,
 object)

b controls a*b because type(b) is a subclass of type(a). b.__rmul__ is used.

np.complex128 is also a subclass of complex.

With integers, not implemented is the factor

In [113]: c = 1; d = np.int64(1)
In [114]: type(c*d)
Out[114]: numpy.int64
In [115]: c.__mul__(d)
Out[115]: NotImplemented
In [116]: d.__rmul__(c)
Out[116]: 1
In [117]: type(_)
Out[117]: numpy.int64
In [118]: type(d).__mro__
Out[118]: 
(numpy.int64,
 numpy.signedinteger,
 numpy.integer,
 numpy.number,
 numpy.generic,
 object)

np.int64 is not a subclass of int.

Post a Comment for "Confusing Type Of Results Of Binary Arithmetic Operations Involving Python Builtin Numeric Types And Numpy Types Float64 And Complex128"