[英]What is the relationship between Python 3 built-in types?
In python 3 everything is a object. 在python 3中,一切都是对象。 I drew a diagram about relation of classes.
我画了一个关于类关系的图。 Is this diagram correct?
这个图正确吗?
the hard part is about type and object classes. 困难的部分是关于类型和对象类的。 how are they in relation ?
他们之间的关系如何? type is a object ?
类型是对象? or object is a type ?
还是对象是类型?
>>> x=type
>>> type(x)
<class 'type'>
>>> x=object
>>> type(x)
<class 'type'>
As far as I know, the class relations are kind of like this in Python 3: 据我所知,类关系在Python 3中有点像这样:
object
object
的子类 type
type
的实例 Every class is created by the type
class or an other metaclass, which derives from type
. 每个类都是由
type
类或另一个从type
派生的元类创建的。 Because of this every class is an instance of type
(including type
!) Every class will return True
for isinstance(cls, type)
. 因此,每个类都是
type
(包括type
!)的实例。每个类将为isinstance(cls, type)
返回True
。
In Python 3, every class is also a subclass from object
. 在Python 3中,每个类也是
object
的子类。 Every class or instance will return True
for isinstance(cls_or_instance, object)
每个类或实例将为
isinstance(cls_or_instance, object)
返回True
。
A special case is metaclasses. 元类是一个特例。 A metaclass derives from
type
, so every metaclass will return True
for issubclass(metaclass, type)
and isinstance(metaclass, type)
一个元类是从
type
派生的,因此每个元类将为issubclass(metaclass, type)
和isinstance(metaclass, type)
返回True
。
The type object is itself an object. 类型对象本身就是一个对象。 Note though that the inheritance model of python is not the same as in other OO-languges, much of it depends on duck typing rather than inheritance.
请注意,尽管python的继承模型与其他OO语言不同,但它很大程度上取决于鸭子的类型而不是继承。
Note that type(x)
returns the type of the object, that type(object)
returns <class 'type'>
means nothing more than the type of object
(which is the type all objects has) is a type
(the type all types are), type
itself is a type so the type of it is again type
. 请注意,
type(x)
返回对象的类型, type(object)
返回<class 'type'>
意味着object
的类型(所有对象都具有的type
)就是type
(所有类型的类型是), type
本身就是类型,所以它的类型又是type
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.