[英]Why is `object` an instance of `type` and `type` an instance of `object`?
I am a little bit confused about the object
and type
classes in Python 3. Maybe someone can clear up my confusion or provide some additional information.我对
object
和 Python 3 中的type
类有点困惑。也许有人可以澄清我的困惑或提供一些额外的信息。
My current understanding is that every class (except object
) inherits from a base class called object
.我目前的理解是,每个 class (除了
object
)都继承自一个名为object
。 But every class (including object
) is also an instance of the class type
, which is an instance of itself and object
and also inherits from object
. But every class (including
object
) is also an instance of the class type
, which is an instance of itself and object
and also inherits from object
.
My questions are:我的问题是:
Is there a reason/design decision why object
is an instance of type
and type
inherits from object
?是否有理由/设计决定为什么
object
是type
的实例并且type
继承自object
? Has the type
/class of an object also to be an object itself? object 的
type
/类本身是否也是 object 本身?
How can a class ( type
) be an instance of itself? class (
type
)如何成为其自身的实例?
Which one is the real base class object
or type
?哪一个是真正的基础 class
object
或type
?
I always thought object
would be the most "fundamental" class, but it seems to be an instance of type
, which is an instance of object
, which is an instance of type
,... Where does this recursion end?我一直认为
object
会是最“基本”的 class,但它似乎是type
的一个实例,它是object
的一个实例,这是一个 end type
的递归,...
Is there a possibility to illustrate the relation between the object
and the type
class?是否有可能说明
object
和 class type
之间的关系?
I tried looking up the entries of object
and type
in the Documentation of the Python Standard Library.我尝试查找
object
的条目并type
Python 标准库的文档。
Every class (except object) inherits from object.每个 class(对象除外)都继承自 object。
>>> for x in object, int, float, str, list, dict:
... print(f'{x.__name__:6}: {x.__bases__}')
...
object: ()
int : (<class 'object'>,)
float : (<class 'object'>,)
str : (<class 'object'>,)
list : (<class 'object'>,)
dict : (<class 'object'>,)
Every class is an instance of the class type
.每个 class 都是 class
type
的实例。
>>> for x in object, int, float, str, list, dict:
... print(f'{x.__name__:6}: {x.__class__}')
...
object: <class 'type'>
int : <class 'type'>
float : <class 'type'>
str : <class 'type'>
list : <class 'type'>
dict : <class 'type'>
type
is an instance of itself. type
是它自己的一个实例。
>>> type.__class__
<class 'type'>
type
also inherits from object
. type
也继承自object
。
>>> type.__bases__
(<class 'object'>,)
Also还
>>> isinstance(object, type)
True
>>> isinstance(type, object)
True
>>> isinstance(type, type)
True
>>> isinstance(object, object)
True
>>> issubclass(type, object)
True
>>> issubclass(object, type)
False
Answers to all your questions can be found in this book: Python Types and Objects您所有问题的答案都可以在本书中找到: Python 类型和对象
UPD: another link to the book . UPD:本书的另一个链接。 Let me know if it dies too.
如果它也死了,请告诉我。
The most important parts to answer your questions:回答您的问题的最重要部分:
Yes, according to the Rule 1 from chapter 1:是的,根据第 1 章的规则 1:
"Everything is an object... Any classes that we define are objects, and of course, instances of those classes are objects as well." “一切都是 object……我们定义的任何类都是对象,当然,这些类的实例也是对象。”
object
or type
?object
或type
? From chapter 2:从第 2 章开始:
"These two objects are primitive objects in Python. We might as well have introduced them one at a time but that would lead to the chicken and egg problem - which to introduce first? These two objects are interdependent - they cannot stand on their own since they are defined in terms of each other." “这两个对象是 Python 中的原始对象。我们不妨一次只引入一个,但这会导致鸡和蛋的问题——先引入哪个?这两个对象是相互依赖的——因为它们不能独立存在它们是相互定义的。”
Also Luciano Ramalho in his book "Fluent Python" says that this relation can't be expressed in Python (chapter 21): Luciano Ramalho 在他的“Fluent Python”一书中也说这种关系不能用 Python(第 21 章)来表达:
"The classes object and type have a unique relationship: object is an instance of type, and type is a subclass of object. This relationship is "magic": it cannot be expressed in Python because either class would have to exist before the other could be defined. The fact that type is an instance of itself is also magical." "The classes object and type have a unique relationship: object is an instance of type, and type is a subclass of object. This relationship is "magic": it cannot be expressed in Python because either class would have to exist before the other could被定义。类型是自身的一个实例这一事实也很神奇。
So, for your question:所以,对于你的问题:
Luciano says that it can't be expressed in Python too. Luciano 说它也不能用 Python 来表达。
Many thanks to the author who made this illustration in сhapter 3:非常感谢在第 3 章中制作此插图的作者:
<class 'type'>
is the metaclass of class object
, and every class (including type
) has inherited directly or indirectly from object
. <class 'type'>
是 class object
的元类,每个 class (包括type
)都直接或间接继承自object
(包括 type )。
If you need to find more about metaclasses chack out here https://realpython.com/python-metaclasses/如果您需要了解更多关于元类的信息,请点击此处https://realpython.com/python-metaclasses/
According to Python Data Model , everything in Python is an object and every object has an identity, a type and a value. According to Python Data Model , everything in Python is an object and every object has an identity, a type and a value.
object
is the base class for all objects, type
is also an object, so it is an instance of object
, and same for object
itself. object
is the base class for all objects, type
is also an object, so it is an instance of object
, and same for object
itself. Also type
is the base class of all object types, so type of object
is type
, and same for type
itself.同样
type
是所有 object 类型的基础 class ,因此object
的类型是type
,并且type
本身也是如此。
This is my understanding, welcome to point out any mistakes.这是我的理解,如有错误欢迎指出。
I realized it was very similar to instanceKlass and _java_mirror in Java我意识到它与 Java 中的 instanceKlass 和 _java_mirror 非常相似
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.