为什么 int 是 number.Number 的子类? [英] Why int is subclass of number.Number?
本文介绍了为什么 int 是 number.Number 的子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们看看下面的代码:
Let's see the following code:
>>> int.__mro__
(<class 'int'>, <class 'object'>)
>>> import numbers
>>> issubclass(int, numbers.Number)
True
我们可以看到__mro__
中没有Number
,说明int
不是继承自Number
,但是为什么 int
是 Number
的子类?
We can see there is no Number
in __mro__
, that means int
does not inherit from Number
, but why int
is subclass of the Number
?
推荐答案
Number
是一个抽象基类 (ABC).普通类型可以注册为 ABC 的子类.
Number
is an abstract base class (ABC). Normal types can be registered as subclasses of the ABC.
参见 https://docs.python.org/3/library/abc.html#abc.ABCMeta.register :
注册(子类)
将子类注册为此 ABC 的虚拟子类".
Register subclass as a "virtual subclass" of this ABC.
numbers
模块包含行
Integral.register(int)
Integral
是 Rational
的子类真实
<复杂
号码
.
Integral
is a subclass of Rational
< Real
< Complex
< Number
.
因此int
是Number
的虚拟子类.
这篇关于为什么 int 是 number.Number 的子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文