类型和对象原始对象之间的区别 [英] Difference between type and object primitive objects
问题描述
根据我对 Python 3 的理解,类型 type
是用于创建 Class 对象的元类,所以 type
是 object
因为汽车"是捷豹",通过调用它的 __call__
方法然后调用类的 __new__
和 __init__
的过程方法,返回类本身的实例.
在正常层面上,类型和对象之间的关系是完全合理的;一切都是对象(子类object
),一切都有一个类型;对象5
的类型为int
,int
的类型为type
本身.因此 isinstance(5, int)
和 isinstance(int, type)
为真.但是,isinstance(5, type)
不是真的,因为 5
不是类对象,而是类的实例.
因此将对象定义为只有一种类型.但是在非常原始的层面上,对于 type
和 object
类本身,它们之间的关系让我感到困惑.type
是 object
的子类,如其 __bases__
所示,但 object
本身是 的一个实例>类型代码>.
如果描述像 5 这样的东西可以分为肉"和骨头",其中肉"代表它在内存中的实际创建并为其赋值,骨头"代表它应该如何的定义,在其合法的价值观和行为范围中,这是否意味着,在核心,type
负责创建类对象的肉",而 object
负责创建类对象定义骨骼"?如果是这样,如果在这种情况下骨骼"是类 int
,那么创建类的实例的肉"等价物是什么?
Python 中的每个对象类都继承了 object
类的行为.首先,这意味着
无论 x
碰巧绑定到什么,
在 Python 3 中都是 true.这也意味着一切都将继承 object
的方法,除非被更具体的类覆盖.因此,如果 x
或 y
都没有覆盖 ,
方法.x == y
将求助于 object.__eq__
>__eq__
type
在 Python 3 中也是一个对象——它继承自对象:
表示type
是一个对象,它继承了object
类的行为.这就是使人们能够写出像
type.__eq__
继承自 object
类.
但是object
类本身也是一个类型:
表示类object
作为对象继承了type
的行为——type
是对象的元类.
当然type
的元类是type:
而且 object
类也是一个 object
:
当你构造一个类的实例时,类本身负责该类的肉和骨头,但也许可以说区别在于骨头遵循普通类继承,而肉在类之间分裂和元类谱系;而元类也是类对象本身的骨架.
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.
At the normal level, the relation between type and object makes perfect sense; everything is an object (subclasses object
), and everything has a type; the type of the object 5
is int
, and int
's type is type
itself. Hence isinstance(5, int)
and isinstance(int, type)
are true. However, isinstance(5, type)
is not true since 5
is not a class object, it's an instance of a class.
This article: 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
is a subclass of object
, as seen in its __bases__
, but object
itself is an instance of 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"? If so, what is the "meat" equivalent of creating an instance of a class, if the "bones", in this case, is the class int
?
Every class of objects in Python inherits the behaviours of the object
class. Firstly, it means that
>>> isinstance(x, object)
True
will be true in Python 3 no matter what x
happens to be bound to. It also means that everything will also inherit the methods of the object
as such, unless overridden by a more specific class. Thus, x == y
will resort to object.__eq__
if neither of x
or y
overrode the __eq__
method.
type
in Python 3 is also an object - it inherits from object:
>>> isinstance(type, object)
True
It means that type
is an object, and it has inherited the behaviour from the object
class. 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.
But the object
class itself is also a type:
>>> isinstance(object, type)
True
It means that the class object
as an object inherits the behaviour of type
- type
is object's metaclass.
And of course type
's metaclass is type:
>>> isinstance(type, type)
True
And the class object
is an object
too:
>>> 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.
这篇关于类型和对象原始对象之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!