标签:
Objects are structures allocated on the heap. Special rules apply to the use of objects to ensure they are properly garbage-collected.
Objects are never allocated statically or on the stack; they must be accessed through special macros and functions only. (Type objects are exceptions to the first rule; the standard types are represented by statically initialized type objects.)
An object has a ‘reference count‘ that is increased or decreased when a pointer to the object is copied or deleted; when the reference count reaches zero there are no references to the object left and it can be removed from the heap.
An object has a ‘type‘ that determines what it represents and what kind of data it contains. An object‘s type is fixed when it is created. Types themselves are represented as objects; an object contains a pointer to the corresponding type object. The type itself has a type pointer pointing to the object representing the type ‘type‘, which contains a pointer to itself!).
对象是分配在堆上的数据结构.对象在使用的时候有特殊的方法来保证他们会在合适的时候被垃圾回收.
对象永远不会被静态分配空间或者在栈上分配空间;访问它们只能通过特殊的宏和函数.(类型对象不适用于第一条规则,标准对象是由静态初始化的类型对象描绘出来的)
对象有一个类型(type),用来指明它代表什么以及它包含哪些数据.一个对象的类型在它被创建的时候就固定了.类型本身也是以对象的形式呈现的.每个对象都包含一个指向相应类型对象(type object)的指针.类型对象本身也有一个指针指向类型对象,也就是说类型对象也有类型,这个指针其实就是指向它自己.
Objects do not float around in memory; once allocated an object keeps the same size and address. Objects that must hold variable-size data can contain pointers to variable-size parts of the object. Not all objects of the same type have the same size; but the size cannot change after allocation. (These restrictions are made so a reference to an object can be simply a pointer -- moving an object would require updating all the pointers, and changing an object‘s size would require moving it if there was another object right next to it.)
Objects are always accessed through pointers of the type ‘PyObject *‘. The type ‘PyObject‘ is a structure that only contains the reference count and the type pointer. The actual memory allocated for an object contains other data that can only be accessed after casting the pointer to a pointer to a longer structure type. This longer type must start with the reference count and type fields; the macro PyObject_HEAD should be used for this (to accommodate for future changes). The implementation of a particular object type can cast the object pointer to the propertype and back.
对象不会在内存中四处飘动,一旦在内存中分配了空间就会保持不动(地址不变,大小不变).那些必须保存可变尺寸数据的对象可以包含一个指针指向一个可变尺寸的部分.不是所有的相同类型的对象都有相同的大小,但是大小在分配空间后是不可变的.(这些限制条件导致引用一个对象可以仅用一个指针--移动一个对象需要更新所有的指针,改变一个对象的大小需要移动它,如果有另一个对象在它之后的话)
对象总是通过指针 PyObject * 来访问,PyObject类型是一个只包含引用计数和类型指针的数据结构.
标签:
原文地址:http://www.cnblogs.com/olivetree123/p/4708224.html