整数和数字之间的差异。Python中的整数

整数和数字之间的差异。Python中的整数,python,Python,我试图对Python的数据模型有更深入的了解,但我并不完全理解以下代码: >>> x = 1 >>> isinstance(x,int) True >>> isinstance(x,numbers.Integral) True >>> inspect.getmro(int) (<type 'int'>, <type 'object'>) >>> inspect.getmro(

我试图对Python的数据模型有更深入的了解,但我并不完全理解以下代码:

>>> 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'>)
>x=1
>>>isinstance(x,int)
真的
>>>isinstance(x,数字,整数)
真的
>>>检查getmro(内部)
(, )
>>>检查。getmro(数字。积分)
(, ,
, )
基于以上,似乎
int
number.Integral
不在同一层次结构中

从Python参考(2.6.6)中,我看到

整数-这些表示整数(正整数和负整数)数学集合中的元素

int
number.Integral
之间有什么区别?它是否与[34]:number.Integral中的
类型int
类数.Integral有关?
In [34]: numbers.Integral ?
Type:           ABCMeta
Base Class:     <class 'abc.ABCMeta'>
String Form:    <class 'numbers.Integral'>
Namespace:      Interactive
File:           c:\python26\lib\numbers.py
Docstring:
    Integral adds a conversion to long and the bit-string operations.


In [35]: int ?
Type:           type
Base Class:     <type 'type'>
String Form:    <type 'int'>
Namespace:      Python builtin
Docstring:
    int(x[, base]) -> integer


In [36]: type(int) == type (numbers.Integral)
Out[36]: False

In [39]: issubclass(int, numbers.Integral)
Out[39]: True
类型:ABCMeta 基类: 字符串形式: 名称空间:交互式 文件:c:\python26\lib\numbers.py 文档字符串: Integral添加对长字符串和位字符串操作的转换。 在[35]:int? 类型:类型 基类: 字符串形式: 名称空间:Python内置 文档字符串: int(x[,基])->整数 在[36]中:type(int)=type(number.Integral) Out[36]:假 In[39]:issubclass(int,number.Integral) Out[39]:对

积分是一个抽象的基类
int
是ABCMeta
Integral

numbers
的一个子类,它定义了抽象类的层次结构,这些抽象类定义了对数值类型可能进行的操作。看见
int
Integral
之间的区别在于
int
是一种具体类型,它支持
Integral
定义的所有操作。

请允许我添加两件事:

isinstance(x,numbers.Integral)
还包括
long

isinstance(x, int)

没有。
number.Integral
测试将更接近

isinstance(x, (int, long))
在Python2中(Python3永远杀死了
long

我更喜欢使用
numbers.Integral
,因为如果从
int
(或
long
)派生,
作为
numbers.Integral的一部分,它仍然是
True

TLDR:
int

# numbers.py:380 (CPython 3.8)
Integral.register(int)

数字。整数
是整数必须提供的抽象定义
int
是整数的具体实现


isinstance
issubclass
函数不限于继承。例如,它们可以表示如
collections.abc.Iterable

>>> class MyIterable:
...     def __iter__(self): ...
...
>>> issubclass(MyIterable, collections.abc.Iterable)
True
事实上,
isinstance
issubclass
都可以。标准库使用它来定义对具体子类(通过继承)和虚拟子类(通过继承)的支持


虚拟子类通过继承与其ABC无关——因此其方法解析顺序不使用ABC。具体地说,
int
不会继承
数字.Integral的任何方法。但是,它确实独立地实现了
numbers.Integral
所需的所有方法,从而满足了
numbers.Integral
的定义。

我已经阅读了有关numbers的类型层次结构和ABC的内容。事实上,我没有在同一层次结构上看到
int
numbers.Integral
,因为
int
不是从
number.Integral
派生的,而是注册为“虚拟子类”。对的我看到了它们之间的关系,从
numbers.Integral
int
,在
numbers.Integral.\u abc\u registry
中,但是也可能看到关系的另一面(从
int
numbers.Integral
)?@ElenaT:
number.Integral
是一个模型(或概念)
int
是该模型的一个实现。这就是他们之间的关系。继承与问题是正交的。我通读了PEP 3141,但仍然不理解为什么issubclass(int,numbers.Integral)返回true,而inspect.getmro(int)没有将Integral列为基类。在我看来,方法的解析顺序必然包括数字.Integral。因此,我在这一点上的唯一结论是int()的某种内置优化。我无法确定Python“model”或“concept”的含义,但我确实看到int和numbers.Integral都是Python类。如果在Python 2上为
int
(或
long
)子类,
isinstance(subclass_instance,(int,long))
仍然是真的。不同之处在于,如果您使用的是另一种整数类型,它定义了所有相同的方法,但不是从
int
long
继承的,例如numpy整数
isinstance(np.int64(),(int,long)
将始终为False,而
isinstance(np.int64(),numbers.Integral)
将返回True。
numbers.Integral
是一个抽象类,不能实例化它。与
int