简体   繁体   English

类型和对象原始对象之间的区别

[英]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 对象的元类,因此typeobject因为“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.但是在非常原始的级别,类typeobject本身,它们之间的关系让我感到困惑。 type is a subclass of object , as seen in its __bases__ , but object itself is an instance of type . typeobject的子类,如其__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.因此,如果xy都没有覆盖__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.

相关问题 <type'classobj'>,<type'object'>之间的区别? - Difference between <type 'classobj'>, <type 'object'>? django orm 中objects.create() 和object.save() 的区别 - difference between objects.create() and object.save() in django orm 在可变对象和不可变对象的情况下,Python对象引用和变量之间的区别 - Difference between Python object reference and variables in cases of mutable and immutable objects type .__ getattribute__和object .__ getattribute__有什么区别? - What is the difference between type.__getattribute__ and object.__getattribute__? python 中的 class 和 object class 类型有什么区别 - what is the difference between type class and object class in python object 与类型作为基类有什么区别? - What is the difference between object vs type as base classes? 在Python cProfile中,调用计数和原始调用计数有什么区别? - In Python cProfile, what is the difference between calls count and primitive calls count? Primitive、Control 和 Complex Control 小部件有什么区别? - What is the difference between a Primitive, Control and Complex Control widget? 为什么“原始”和“非原始”对象之间存在深浅复制差异? - Reason why deep and shallow copy differences between "primitive" and "non-primitive" objects? python中时间对象之间的区别? - Difference between the time objects in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM