(Tomorrow the paper will be on trial!!!) Distance graduation one months!!! )
Now still remember the first time to learn Python by this dynamic language surprised to the feeling that the list and Dictionary objects can be arbitrarily stretched, simply can not be more handsome, but has not known how the internal exactly how to achieve, Python source code with C implementation, but C is the procedural language ah.
Say how to achieve before, first smoothed out what is the object, for this I think the "Python Source code Anatomy" The interpretation of the book is very interesting: " an object is actually a allocated memory space, these memory may be contiguous, it may be discontinuous, it is not important, The important thing is that there is a higher level of this piece that can be considered as a whole , the whole object, in this memory, a series of data, and the code that can modify and read the data , "a very interesting angle.
When an object is created, the amount of memory it occupies is immutable, which means that for variable-length data (such as a string), only a pointer to a variable-sized area of memory is maintained within the object.
In Python, everything is an object, in which the core of all objects is a struct called pyobject.
1 struct _object{2 int ob_refcnt; 3 struct _typeobject *ob_type; 4 }pyobject;
Where ob_refcnt is the reference count, _typeobject determines the type of the object, Ob_type points to a type Object (My understanding: the base Class), which will appear in the first place where all objects occupy memory, For example, in an int class:
1 struct {2pyobject _head; 3 Long Ob_ival; 4 }pyintobject;
int worth information is saved in the Ob_ival, this is the Int object in the python2.5, if it is a variable length object of list, this method is obviously not good, then the head structure pyobject in addition to a representation of how many objects this object occupies information ob_size , this value also indicates how many elements are in a side-long object:
1 #define Pyobject_var_head2 pyobject_head3 int ob_size; 4 5 struct {6pyobject_var_head7 } Pyvarobject;
So, the memory representation of objects in Python is the following:
As you can see from above, the type of the object is entirely determined by the type object that Ob_type points to, so what is the type object?
struct _typedefobject{pyobject_var_headChar*tp_name; int tp_basicsize,tp_itemsize;destructor tp_dealloc;printfunc tp_print;hashfunc tp_hash;ternaryfunc tp_call; /* ... */ }pytypeobject;
*TP_BASICSIZE,TP_ITEMSIZE Specifies the size of the allocated memory space
* The rest of the pointers to functions with the Func word indicate the operation that this object carries
So, how did the object be created?
First of all, in Python's new class, the base class for all objects is object, just as the base class for all objects in Java is the same as the single-root class System of object, which means that all objects automatically inherit all the methods of their base class.
When you need to create an object, such as creating an Pyint_type object to use for an int object, look for the tp_new pointer in the Pyint_type class, and if the pointer is null, look for its parent class, such as the Tp_new pointer in object. Use this pointer to the function to create the object, it points to the function will first look at the Tp_basicsize field in Pyint_type, according to the information of this field to determine how much memory for this object, when the creation is complete, will find Tp_init in Pyint_type, Initializes the memory allocated by tp_new with the function it points to. I understand that tp_new is equivalent to the __new__ static class method in the Python class, Tp_init is equivalent to the __init__ method.
As you can see, there are many pointers to functions in pytypeobject, and these function pointers determine the behavior of the object, with three very important function pointers pointing to three operations families:
Tp_as_number-------->pynumbermethods
tp_as_sequence------>pysequencemethods (list)
tp_as_mapping------>pymappingmethods (dictionary dict)
For each object, you can point to three operating families, which can be implemented by hooks, such as __getitem__,__setitem__
Reference: Python Source code anatomy
[Python] Source code-object creation and behavior