Answer a question

I'm trying to get a deeper understanding in Python's data model and I don't fully understand the following code:

>>> x = 1

>>> isinstance(x,int)
True

>>> isinstance(x,numbers.Integral)
True

>>> inspect.getmro(int)
(<type 'int'>, <type 'object'>)

>>> inspect.getmro(numbers.Integral)
(<class 'numbers.Integral'>, <class 'numbers.Rational'>, <class 'numbers.Real'>,
 <class 'numbers.Complex'>, <class 'numbers.Number'>, <type 'object'>)

Based on the above, it seems that int and number.Integral are not in the same hierarchy.

From the Python reference (2.6.6) I see

numbers.Integral - These represent elements from the mathematical set of integers (positive and negative).

What's the difference between int and numbers.Integral? Does it have something to do with the type int vs class numbers.Integral I see in the above output?

Answers

numbers defines a hierarchy of abstract classes that define operations possible on numeric types. See PEP 3141. The difference between int and Integral is that int is a concrete type that supports all the operations Integral defines.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐