Python object in python source code analysis notes

Source: Internet
Author: User

 

 

1. in python, the object is a piece of memory applied for on the heap for the struct in C. Objects cannot be statically initialized, nor can they survive in the stack space. However, all built-in type objects are statically initialized.

2. The object size remains unchanged after being created. A variable object has a pointer pointing to a variable-size memory area.

3. The cornerstone of the object mechanism: PyObject

Fixed Length object:

typedef struct _object{           PyObject_HEAD}PyObject;

Release mode Compilation -->

 

Typedef struct _ object {intob_refcnt; // reference count, garbage collection mechanism struct_typeobject * ob_type; // type information, specified object type} PyObject;

PyObject is a total of python objects, and the most initial bytes of the object in memory.

Variable-length object:

 

# Define PyObject_VAR_HEAD PyObject_HEAD contains ob_size; // The number of elements that can be accommodated in the Variable Length section. typedef struct {PyObject_VAR_HEAD} PyVarObject;

In Python, each object has the same object header. We only need to use a PyObject * pointer to reference any object.

Eg:

 

// Pyobjecbject object typedef struct {PyObject_HEAD longob_ival; // save value} // pythonStringObject typedef struct {symbol longob_shash; into B _sstate; charob_sval [1];} PyStringObject; // PyListObject object typeDef struct {struct PyObjct ** ob_item; intallocated;} PyListObject; // PyDictObject object typedef struct {struct; PyObject * me_key; PyObject * me_value;} PyDictEntry;

4. Type object (_ typeobject type --> Implementation of the "class" concept in object-oriented theory)

 

Typedef struct _ typeobject {PyObject_VAR_HEAD // The type object is also an object, and it is also a variable-length object char * tp_name; // type name, inttp_basicsize, tp_itemsize will be used for printing; // The size of memory occupied by this type. The/* operation information associated with this type of object */destructor tp_dealloc; printfunc tp_print; //…} will be used for memory allocation ;//...} PyTypeObject;

PyType_Type (metaclass), PyInt_Type, PyString_Type, PyDict_Type, and PyList_Type are the instantiation objects of _ typeobject.

5. Reference count

 

Py_INCREF (op) // Add an object reference count Py_DECREF (op) // reduce the reference count of an object

When the reference count is reduced to 0, Py_DECREF calls the destructor of the object to release the memory and system resources occupied by the object. This destructor is specified by a function pointer defined in the corresponding type object of the object, that is, tp_dealloc

Calling the Destructor does not mean that the free space will be called in the end. In Python, a large number of Memory Object pool technologies are used to avoid frequent application and release of memory space.

Type objects will never be destructed.

6. Create an object

6.1 Python c api (this method is generally used for built-in types)

 

// APIPyObject * parameter bj = PyObject_New (PyObject, & PyInt_Type); // APIPyObject * parameter bj = PyInt_FromLong (10)

 

6.2 type object PyInt_Type (the custom type can only be in this way)

Int (10) is an integer object created through PyInt_Type.

All classes are object-based

Tp_new --> apply for memory corresponding to the new operator in C ++

Tp_init --> corresponding to the constructor in C ++, complete the "initialization" Object operation

7. Object Behavior

PyTypeObject defines a large number of function pointers, such as tp_new, tp_init, tp_dealloc, and tp_hash. Three of them are very important operation families.

Tp_as_number --> PyNumberMethods

Tp_as_sequence --> PySequenceMethods

Tp_as_mapping --> PyMappingMethods

 

typedef PyObject *(*binaryfunc) (PyObject *, PyObject *);typedef struct{binaryfunc nb_add;binaryfunc nb_subtract;//…} PyNumberMethods;typedef struct {lenfunc sq_length;binaryfunc sq_concat;ssizeargfunc sq_repeat;ssizeargfunc sq_item;ssizessizeargfunc sq_slice;ssizeobjargproc sq_ass_item;ssizessizeobjargproc sq_ass_slice;objobjproc sq_contains;/* Added in release 2.0 */binaryfunc sq_inplace_concat;ssizeargfunc sq_inplace_repeat;} PySequenceMethods;typedef struct {lenfunc mp_length;binaryfunc mp_subscript;objobjargproc mp_ass_subscript;} PyMappingMethods;

 

 

8. Type (PyType_Type)

The PyTypeObject corresponding to the custom class is created through PyType_Type. All classes are PyType_Type.

 

PyTypeObject PyType_Type = {           PyObject_HEAD_INIT(&PyType_Type)           0,           “type”,           //…};

Relationship between a common type object and a PyType_Type object

 

9. Python object Polymorphism

Through PyObject and PyTypect, Python uses the C language to complete the polymorphism of the objects provided by C ++. Python uses a PyObject * variable instead of a py1_bject * variable (or other specific types) to maintain this object. We don't know what type the pointer refers, however, it can be dynamically determined by the ob_type field of the object.

Eg.

 

Void Print (PyObject * object) {object-> ob_type-> tp_print (object); // call the corresponding tp_print Based on the object type. A function shows different behaviors under different circumstances, that is, polymorphism}


 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.