Int Object in Python
Contents
Preparation
Here, we are goint to analysis how Python deal with Int Object
in itself.
First of all, we should have a environment which help us to explore the detail of the implementation in Python 2.7
. At the same time, our operation shouldn’t influence the origianl Python in your workstation, if you have install Python before.
You can get the source code from offical website of Python. And you will find that there have a file configure
in the directory of the source.
In the file configure, you can replace all string ‘Python’ with ‘pyLab’. And then move all file prefix with Python
into pyLab
(mainly in directory Misc/ and Modules)
Don’t forget to run the configuration file – configure with the option --prefix=Location where you want the Python which is for lab to install
Finally, just run make & make install
Repeat myself again, the reason that I move all ‘Python’ into ‘pyLab’ is all for making a difference with the original instance which is official and it shouldn’t be influence by any operation from us.
Rules of Object in Python.
- Objects are structures allocated on the heap.
- Objects are never allocated statically or on the stack; they must be accessed through special macros and functions only.
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!).
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.
Objects are always accessed through pointers of the type ‘PyObject *‘.
Firt glance at the implementation.
|
|
Actually, PyObject are created by Macro PyObject_HEAD
|
|
Typically, the Int Object in Python PyIntObject
is a immutable object
. Once we created the object, we can’t change the value in that object.
Back to our topic, let’s look at the definition of PyIntObject
.
|
|
Aha, the PyIntObject
is just a simple capsulation of long
in C.
This object maintain a long data member.
|
|
How about the detail of printing a integer in Python ?
|
|
There have many others method which are defined in PyNumberMethods
. Python programmer can get the information about the object with a data member __doc__
, like this:
The implementation of that is just a plain text string in Object/intobject.c
|
|
Initialization of PyIntObject
Python support many different API to construct a PyIntObject
.
|
|
Before we dig into the implementation of the initialization process.
Let’s do a lab.
id(object)
will returen the address of object in CPython.(If you are interesting in the implementation
of built-in method id(object), you can jump to the end of this article and I presented how CPython implement this function.)
Try to answer the following question.
Why int object m
and n
of value 1000
have different address?
Why int object x
and y
of value 1
have the same address?
The answer relate to the mechanism with numbers in Python.
In daily programming stuff, small number are used frequently. But large number may used frequent more than small numbers
If there isn’t a special mechanism, Python will allocate memory(call malloc() in C) again and again. This stratege isn’t efficient at run time.
So, Python support a mechanism which will create small number only once but not bigger number.
There also have the other question. What means big? What means small?
It’s not clearly in theory. But in the implementation, there have a trade off. People can’t cache all integer number for the limitation of memory(RAM).
|
|
According to the implementation, the small number is [-5, 257). You can also modify the source code and recomplie it to change the range of small number, if you would like to.
Integers are quite normal objects, to make object handling uniform.
(Using odd pointers to represent integers would save much space
but require extra checks for this special case throughout the code.)
Since a typical Python program spends much of its time allocating
and deallocating integers, these operations should be very fast.
Therefore we use a dedicated allocation scheme with a much lower
overhead (in space and time) than straight malloc(): a simple
dedicated free list, filled when necessary with memory from malloc().
block_list is a singly-linked list of all PyIntBlocks ever allocated, linked via their next members. PyIntBlocks are never returned to the
system before shutdown (PyInt_Fini).
free_list is a singly-linked list of available PyIntObjects, linked
via abuse of their ob_type members.
|
|
Insert and Delete
A initialization routine from long type
integer number.
|
|
Time for Hacking
Modify the int_print
function in Object/intobject.c
.
Here is my modification:
|
|
Tips for debugging:getrefcount
in module sys
is helpful to get the reference number of object.
Extention reading
There are lots of built-in methods in Python. id(Object) is one of them.
According to the build-in doc for function id(Object).
In [1]: print id.__doc__
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it’s the object’s memory address.)
There is a hint and say the return value of built-in function id(object) is the object’s memory address.
All the function do is just 5-line codes in the implementation of CPython.
|
|
That’s all about the built-in method id(Object)
Photo by Jason Leaster, in ChangeDe Hunan
作者: Jason Leaster
来源: http://jasonleaster.github.io
链接: http://jasonleaster.github.io/2016/02/03/memory-model-of-int-object-in-python/
本文采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可