Alex Flint
Alex Flint

Reputation: 6726

Why does "isinstance(numpy.float64,numbers.Real)" return False?

Under numpy 1.8.0, python 2.7.6, I tried running the following code:

>>> isinstance(numpy.float64, numbers.Real)
False

My understanding was that the numbers module is supposed to be a general purpose way to categorize objects that have various number-like functionality. numpy.float64 certainly has the semantics of a real number, so why haven't the numpy developers chosen to register numpy.float64 with the numbers ABC?

I can of course do the registration myself but I just wanted to check whether there is some good reason for this omission.

Upvotes: 2

Views: 891

Answers (1)

user2555451
user2555451

Reputation:

You are not creating an instance of numpy.float64:

>>> import numpy  
>>> import numbers
>>> numpy.float64
<class 'numpy.float64'>
>>> numpy.float64()
0.0
>>> isinstance(numpy.float64, numbers.Real)
False
>>> isinstance(numpy.float64(), numbers.Real) # Notice the () after numpy.float64
True
>>>

Instead, your current code is simply checking if the numpy.float64 class itself is an instance of numbers.Real. This will never be true.


Note that you get the same behavior for any number type:

>>> import numbers
>>> isinstance(int, numbers.Real)
False
>>> isinstance(int(), numbers.Real)
True
>>> isinstance(float, numbers.Real)
False
>>> isinstance(float(), numbers.Real)
True
>>>

Upvotes: 7

Related Questions