本文介绍了为什么`object`是`type`的实例而`type`是`object`的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Python 3 中的 objecttype 类有点困惑.也许有人可以解决我的困惑或提供一些额外的信息.

我目前的理解是每个类(object 除外)都继承自一个名为 object 的基类.但是每一个类(包括object)也是type类的一个实例,它是自身和object的一个实例,也继承自.

我的问题是:

  • 为什么objecttype 的实例并且type 继承自object?对象的 type/class 是否也是对象本身?

  • 一个类(type)如何成为它自己的一个实例?

  • 哪个是真正的基类objecttype?
    一直以为object会是最基础"的类,但好像是type的实例,也就是object的实例,它是 type 的一个实例,...这个递归到哪里结束?

  • 有没有可能说明objecttype类之间的关系?

我尝试在 Python 标准库的文档中查找 objecttype 的条目.

每个类(对象除外)都继承自对象.

>>>对于对象、int、float、str、list、dict 中的 x:...打印(f'{x.__name__:6}: {x.__bases__}')...目的: ()int : (,)浮动 : (,)str : (,)列表 : (,)dict : (,)

每个类都是type类的一个实例.

>>>对于对象、int、float、str、list、dict 中的 x:...打印(f'{x.__name__:6}: {x.__class__}')...对象:<类'类型'>int : <类'类型'>浮动:<类'类型'>str : 列表:<类'类型'>dict : 

type 是自身的一个实例.

>>>类型.__类__

type 也继承自 object.

>>>类型.__基数__(<类'对象'>,)

还有

>>>isinstance(对象,类型)真的>>>isinstance(类型,对象)真的>>>isinstance(类型,类型)真的>>>isinstance(对象,对象)真的
>>>issubclass(类型,对象)真的>>>issubclass(对象,类型)错误的
解决方案

所有问题的答案都可以在本书中找到:

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.

My current understanding is that every class (except object) inherits from a base class called 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? Has the type/class of an object also to be an object itself?

  • How can a class (type) be an instance of itself?

  • Which one is the real base class object or 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?

  • Is there a possibility to illustrate the relation between the object and the type class?

I tried looking up the entries of object and type in the Documentation of the Python Standard Library.

Every class (except object) inherits from 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.

>>> 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.__class__
<class 'type'>

type also inherits from 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

UPD: another link to the book. Let me know if it dies too.

The most important parts to answer your questions:

  • Has the type/class of an object also to be an object itself?

Yes, according to the Rule 1 from chapter 1:

"Everything is an object... Any classes that we define are objects, and of course, instances of those classes are objects as well."

  • Which one is the real base class object or type?

From chapter 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."

Also Luciano Ramalho in his book "Fluent Python" says that this relation can't be expressed in Python (chapter 21):

"The classes object and type have a unique relationship: object is aninstance of type, and type is a subclass of object. This relationshipis "magic": it cannot be expressed in Python because either class wouldhave to exist before the other could be defined. The fact that type isan instance of itself is also magical."

So, for your question:

  • How can a class (type) be an instance of itself?

Luciano says that it can't be expressed in Python too.

  • Is there a possibility to illustrate the relation between the object and the type class?

Many thanks to the author who made this illustration in сhapter 3:

这篇关于为什么`object`是`type`的实例而`type`是`object`的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 20:16