Continuous numeric type checking

Leave the ducks in the pond.

Just to be clear, I'm working with Python 2.7.3.

I played with number checks and came across a few things that I found odd:

In [1]: numbers.Number.mro()
Out[1]: [numbers.Number, object]

In [2]: numbers.Complex.mro()
Out[2]: [numbers.Complex, numbers.Number, object]

In [3]: numbers.Real.mro()
Out[3]: [numbers.Real, numbers.Complex, numbers.Number, object]

In [4]: numbers.Rational.mro()
Out[4]: [numbers.Rational, numbers.Real, numbers.Complex,
         numbers.Number, object]

In [5]: numbers.Integral.mro()
Out[5]: [numbers.Integral, numbers.Rational, numbers.Real,
         numbers.Complex, numbers.Number, object]

It seems to me ... counterproductive and somewhat controversial within the the Python ( int, float, complexsimply inherits objectdirectly):

In [6]: isinstance(int(), complex)
Out[6]: False

In [7]: isinstance(int(), numbers.Complex)
Out[7]: True

Then I wrote the following function:

def numeric_check(num):
    print "Is an int:", isinstance(num, int)
    print "Is a float:", isinstance(num, float)
    print "Is a complex:", isinstance(num, complex)
    print "Is a numbers.Number:", isinstance(num, numbers.Number)
    print "Is an numbers.Integer:", isinstance(num, numbers.Integral)
    print "Is a numbers.Real:", isinstance(num, numbers.Real)
    print "Is a numbers.Complex:", isinstance(num, numbers.Complex)
    print "Is a numpy.integer:", isinstance(num, numpy.integer)
    print "Is a numpy.floating:", isinstance(num, numpy.floating)
    print "Is a numpy.complex:", isinstance(num, numpy.complex)

And launched the following loop:

for dtype in [int, float, complex,
              numpy.int16, numpy.int32, numpy.int64,
              numpy.uint16, numpy.uint32, numpy.uint64,
              numpy.float16, numpy.float32, numpy.float64, numpy.complex64]:
    num = dtype()
    print dtype
    numeric_check(num)

I will spare you the full conclusion, but a few excerpts:

    type 'int'
    Is an int: True
    Is a float: False
    Is a complex: False
    Is a numbers.Number: True
    Is an numbers.Integer: True
    Is a numbers.Real: True
    Is a numbers.Complex: True
    Is a numpy.integer: False
    Is a numpy.floating: False
    Is a numpy.complex: False

, , int numbers. numpy 64 , :

    type 'numpy.int64'
    Is an int: True
    Is a float: False
    Is a complex: False
    Is a numbers.Number: True
    Is an numbers.Integer: True
    Is a numbers.Real: True
    Is a numbers.Complex: True
    Is a numpy.integer: True
    Is a numpy.floating: False
    Is a numpy.complex: False

, int, numpy.integer. numpy.int16:

    type 'numpy.int16'
    Is an int: False
    Is a float: False
    Is a complex: False
    Is a numbers.Number: False
    Is an numbers.Integer: False
    Is a numbers.Real: False
    Is a numbers.Complex: False
    Is a numpy.integer: True
    Is a numpy.floating: False
    Is a numpy.complex: False

Ouch, numpy.integer. , :

  • ?
  • numpy ?
  • , , , numbers ?

:

isinstance(num, (int, numpy.integer)
isinstance(num, (float, numpy.floating)
isinstance(num, (complex, numpy.complex)
+4
1

, numpy.int * as Integral:

import numpy as np
import numbers
numbers.Integral.register(numpy.integer)
a = np.int16(100)
isinstance(a, numbers.Integral)

numpy.int16 , int, numpy.int16, .

+5

Source: https://habr.com/ru/post/1533969/


All Articles