Python 3:对象如何成为类型的实例? [英] Python 3: How can object be instance of type?
问题描述
在 Python 3 中,object
是 type
的实例,type
也是 object
的实例!
In Python 3, object
is an instance of type
and type
is also an instance of object
!
每个类怎么可能派生自另一个?
How is it possible that each class is derived from the other?
任何实施细节?
我使用 isinstance(sub, base)
检查了这个,根据 Python 文档,它检查子类是否从基类派生:
I checked this using isinstance(sub, base)
, which, according to Python documentation, checks if sub class is derived from base class:
isinstance(object, type)
Out[1]: True
isinstance(type, object)
Out[2]: True
推荐答案
这是 Python 中的边缘情况之一:
This is one of the edge cases in Python:
- Python 中的一切都是对象,所以由于
object
是一切的基本类型,type
(Python 中的东西)是object
. - 由于
object
是一切的基础type,object
也是type,这使得object
type
的一个实例.
- Everything in Python is an object, so since
object
is the base type of everything,type
(being something in Python) is an instance ofobject
. - Since
object
is the base type of everything,object
is also a type, which makesobject
an instance oftype
.
请注意,这种关系是无法用 Python 中自己的事物复制的.这是语言中内置的一个例外.
Note that this relationship is nothing you can replicate with your own things in Python. It’s a single exception that is built into the language.
在实现方面,这两个名称分别用PyBaseObject_Type
(对于object
)和PyType_Type
(对于type
)表示代码>).
On the implementation side, the two names are represented by PyBaseObject_Type
(for object
) and PyType_Type
(for type
).
当您使用 isinstance
时,类型检查——在最后一步,在其他一切都失败后——由 type_is_subtype_base_chain
:
When you use isinstance
, the type check—in the very last step, after everything else has failed—is done by type_is_subtype_base_chain
:
type_is_subtype_base_chain(PyTypeObject *a, PyTypeObject *b)
{
do {
if (a == b)
return 1;
a = a->tp_base;
} while (a != NULL);
return (b == &PyBaseObject_Type);
}
这本质上是在 a
的类型层次结构上继续向上,并根据 b
检查结果类型.如果它找不到,那么最后的方法是检查 b
是否实际上是 object
在这种情况下函数返回 true:因为一切都是对象.所以一切都是object
的实例"部分实际上被硬编码到实例检查中.
This essentially keeps going up the type hierarchy of a
and checks the resulting type against b
. If it cannot find one, then the last resort is to check whether b
is actually object
in which case the function returns true: since everything is an object. So the "everything is an instance of object
" part is actually hardcoded into the instance check.
至于为什么 object
是 type
,这实际上更简单,因为它在 PyBaseObject_Type
的声明:
And as for why object
is a type
, this is actually even simpler because it’s simply defined that way in the declaration of PyBaseObject_Type
:
PyTypeObject PyBaseObject_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"object", /* tp_name */
sizeof(PyObject), /* tp_basicsize */
…
PyVarObject_HEAD_INIT
主要设置核心类型信息,包括基本类型,即PyType_Type
.
The PyVarObject_HEAD_INIT
essentially sets the core type information stuff, including the base type, which is PyType_Type
.
这种关系实际上还有两个后果:
There are actually two more consequences of this relationship:
- 既然一切都是对象,
object
也是object
的一个实例:isinstance(object, object)
- 由于
PyType_Type
也是用相同的PyVarObject_HEAD_INIT
实现的,所以type
也是一种类型:isinstance(type, type)
.
- Since everything is an object,
object
is also an instance ofobject
:isinstance(object, object)
- Since
PyType_Type
is also implemented with the samePyVarObject_HEAD_INIT
,type
is also a type:isinstance(type, type)
.
这篇关于Python 3:对象如何成为类型的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!