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

问题描述

我对Python 3中的objecttype类有些困惑.也许有人可以消除我的困惑或提供一些其他信息.

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除外)都继承自名为object的基类.但是每个类(包括object)也是类type的实例,该类是其自身和object的实例,并且还继承自object.

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.

我的问题是:

  • 是否有原因/设计决策,为什么objecttype的实例,而type继承自object?对象的type/class是否也可以是对象本身?

  • 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?

一个类(type)如何成为其自身的实例?

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

哪个是真正的基类objecttype?
我一直以为object是最基础"的类,但它似乎是type的实例,它是object的实例,它是type的实例,...递归在哪里结束?

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?

是否有可能说明objecttype类之间的关系?

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

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

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'>,)

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

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是它本身的一个实例.

type is an instance of itself.

>>> type.__class__
<class 'type'>

type也是从object继承的.

>>> type.__bases__
(<class 'object'>,)

>>> isinstance(object, type)
True
>>> isinstance(type, object)
True
>>> isinstance(type, type)
True
>>> isinstance(object, object)
True
>>> issubclass(type, object)
True
>>> issubclass(object, type)
False

推荐答案

在本书中可以找到您所有问题的答案: Python类型和对象

Answers to all your questions can be found in this book: Python Types and Objects

回答问题的最重要部分:

The most important parts to answer your questions:

  • 对象的类型/类也可以是对象本身吗?

是的,根据第1章的规则1

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

  • 哪个是真正的基类objecttype?

第2章:

这两个对象是Python中的原始对象.我们可能一次也引入了它们,但这会导致鸡肉和鸡蛋问题-首先要引入?这两个对象是相互依赖的-它们不能站立彼此定义,因为它们是彼此定义的."

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

卢西亚诺·拉玛略(Luciano Ramalho)在他的《流利的Python》一书中说,这种关系不能用Python表示(第21章):

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

"class对象和类型具有唯一的关系:object是一个类型的实例,类型是对象的子类.这种关系是魔术":无法用Python表达,因为任何一个类都可以必须先存在,然后才能定义另一个.类型是事实本身的实例也是神奇的."

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

所以,对于您的问题:

  • 类(类型)如何成为其自身的实例?

Luciano说它也不能用Python表达.

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

  • 是否有可能说明对象与类型类之间的关系?

非常感谢在第3章中做出此插图的作者.

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

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

07-30 20:16