[英]Difference between type and object primitive objects
From what I understand of Python 3, the type type
is the meta-class that is used to create Class objects, so type
is to object
as "Car" is to "Jaguar", through a process of calling its __call__
method which then calls the class's __new__
and __init__
methods, returning the instance of the class itself.根据我对 Python 3 的理解,类型type
是用于创建 Class 对象的元类,因此type
是object
因为“Car”是“Jaguar”,通过调用其__call__
方法然后调用的过程类的__new__
和__init__
方法,返回类本身的实例。
At the normal level, the relation between type and object makes perfect sense;在正常层面上,类型和对象之间的关系是完全合理的; everything is an object (subclasses object
), and everything has a type;一切都是一个对象(子类object
),一切都有一个类型; the type of the object 5
is int
, and int
's type is type
itself.对象5
的类型是int
,而int
的类型是type
本身。 Hence isinstance(5, int)
and isinstance(int, type)
are true.因此isinstance(5, int)
和isinstance(int, type)
为真。 However, isinstance(5, type)
is not true since 5
is not a class object, it's an instance of a class.然而, isinstance(5, type)
不是真的,因为5
不是一个类对象,它是一个类的实例。
This article: http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html#object-type-example本文: http : //www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html#object-type-example
Hence defines an object as having exactly one type.因此,将对象定义为仅具有一种类型。 But at the very primitive level, of the classes type
and object
themselves, the relationship between them confuses me.但是在非常原始的级别,类type
和object
本身,它们之间的关系让我感到困惑。 type
is a subclass of object
, as seen in its __bases__
, but object
itself is an instance of type
. type
是object
的子类,如其__bases__
,但object
本身是type
的实例。
If describing something like 5 can be split into "meat" and "bones", where "meat" represents the actual creation of it in memory and assigning values to it, and "bones" the definition of how it should be, of its legal range of values and behaviours, does this mean that, at the very core, type
is responsible for creating the "meat" of class objects, and object
is responsible for defining the "bones"?如果描述像 5 这样的东西可以分为“肉”和“骨头”,其中“肉”代表它在内存中的实际创建并为其赋值,“骨头”代表它应该如何的定义,它的合法性值和行为的范围,这是否意味着,在最核心, type
负责创建类对象的“肉”,而object
负责定义“骨骼”? If so, what is the "meat" equivalent of creating an instance of a class, if the "bones", in this case, is the class int
?如果是这样,如果在这种情况下,“骨骼”是类int
,那么与创建类的实例的“肉”等价物是什么?
Every class of objects in Python inherits the behaviours of the object
class. Python 中的每个对象类都继承了object
类的行为。 Firstly, it means that首先,这意味着
>>> isinstance(x, object)
True
will be true in Python 3 no matter what x
happens to be bound to.无论x
碰巧绑定到什么,在 Python 3 中都是 true。 It also means that everything will also inherit the methods of the object
as such, unless overridden by a more specific class.这也意味着一切都将继承object
的方法,除非被更具体的类覆盖。 Thus, x == y
will resort to object.__eq__
if neither of x
or y
overrode the __eq__
method.因此,如果x
或y
都没有覆盖__eq__
方法,则x == y
将求助于object.__eq__
。
type
in Python 3 is also an object - it inherits from object: Python 3 中的type
也是一个对象——它继承自对象:
>>> isinstance(type, object)
True
It means that type
is an object, and it has inherited the behaviour from the object
class.这意味着type
是一个对象,它继承了object
类的行为。 That is what enables one to write things like这就是使人们能够写出这样的东西的原因
>>> type == type
True
>>> type.__eq__
<slot wrapper '__eq__' of 'object' objects>
The type.__eq__
was inherited from the object
class. type.__eq__
继承自object
类。
But the object
class itself is also a type:但是object
类本身也是一种类型:
>>> isinstance(object, type)
True
It means that the class object
as an object inherits the behaviour of type
- type
is object's metaclass.这意味着类object
作为对象继承了type
的行为—— type
是对象的元类。
And of course type
's metaclass is type:当然type
的元类是 type:
>>> isinstance(type, type)
True
And the class object
is an object
too:类object
也是一个object
:
>>> isinstance(object, object)
True
When you construct an instance of class, the class itself is responsible for both the meat and bones for that class, but perhaps one could put the distinction in that the bones follows the ordinary class inheritance, and meat is split between the class and the metaclass lineage;当你构造一个类的实例时,类本身负责该类的肉和骨头,但也许可以把区别在于,骨头遵循普通的类继承,而肉在类和元类之间分裂血统; and the metaclass is also the bones for the class object itself.元类也是类对象本身的骨架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.