简体   繁体   English

哪个 class 先写在 Python: type 还是 object?

[英]Which class was written first in Python: type or object?

All classes are subclasses of the object and at the same time they all are instances of the type .所有类都是object的子类,同时它们都是type的实例。 Therefore, the object is an instance of the type , and the type is an subclass of the object .因此, objecttype的实例, typeobject的子类。 But, obviously, one of these connections is fictitious.但是,显然,这些联系之一是虚构的。 Which class in the implementation of python is written first? python的实现中先写哪个class?

Both were written at the same time, when the "new style classes" were created, during the development cycle that ended up in Python 2.2.两者是同时编写的,在创建“新样式类”时,在最终以 Python 2.2 结束的开发周期中。 The development of that version made explicit and formalized the metaclass mechanism, and object and type are bound from their inception.该版本的开发明确并形式化了元类机制,并且objecttype从一开始就被绑定。

While it is impossible for one, in plain (or even arcane) Python code to create a class that is both a supertype of your class and an instance of it, as object is to type , when coding the classes in lower level, in the case of cPython, in the C source code of the language, that is perfectly possible: both are statically declared, and the plain forward reference used by the C pre-processor is enough to be able to refer to object in type 's slots before object 's code is declared and vice versa. While it is impossible for one, in plain (or even arcane) Python code to create a class that is both a supertype of your class and an instance of it, as object is to type , when coding the classes in lower level, in the以 cPython 为例,在该语言的 C 源代码中,这是完全可能的:两者都是静态声明的,并且 C 预处理器使用的普通前向引用足以能够在type中引用object之前的插槽 ZA8CFDE63131BD59EB2AC966F8' object的代码已声明,反之亦然。

You may want to read some posts from the time the new style classes where created, and this is a good starting point: https://www.python.org/download/releases/2.2.3/descrintro/您可能想从创建新样式类时开始阅读一些帖子,这是一个很好的起点: https://www.python.org/download/releases/2.2.3/descrintro/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM