Can someone explain the logic behind the output of the following script?
import numpy
if(numpy.dtype(numpy.float64) == None):
print \"Surprise!!!!\"
Looks like an unfortunate accident: someone decided that dtype(None) would "default" to float (though dtype() is an error). Then someone else wrote dtype.__eq__ such that it converts its second argument to a dtype before comparing. So dtype(float) == None is dtype(float) == dtype(None) which is true.
You can see a comment in the source code here: descriptor.c#L1217
- Get typenum from an object -- None goes to NPY_DEFAULT_TYPE
And of course NPY_DEFAULT_TYPE is float (at least usually).
As for the __eq__ operator, it's here: descriptor.c#L3317. It does what I outlined:
if (!PyArray_DescrCheck(other)) {
if (PyArray_DescrConverter(other, &new) == NPY_FAIL) {
return NULL;
}
}
So that's a conversion from whatever is on the right-hand side of == to a dtype object, via the converter function mentioned before, which turns None into dtype(float).
Edit: I found this pretty interesting and it seems like an accident, so I created a patch and submitted to the maintainers: https://github.com/numpy/numpy/pull/4532 .