Just beginning to learn python, found a very confusing phenomenon, has been to read the source code only to know:
is btrue
To initialize two objects with the same parameters, the result is that the two objects are actually the same object???? You kidding me?
is Bfalse
What's going on here? Why is it now false again???
These differences, mostly from Python, are different in how they are initialized for small integers and large integers, and they are all in the object pool, except that they are in a different pool of objects ...
So, what is a small integer and what is a large integer? In Python, the default -5~256 is a small integer, the rest is a large integer ...
is btrue is Bfalse
So the question comes again, why distinguish between small integers and large integers?
Because if you keep calling malloc and free, it can seriously affect the performance of Python, in order to reduce the number of malloc and the free, Python will pre-initialize all small integer objects, that is, when we use small integer object, not created, not initialized, Instead, we get a reference to a small integer object that's already in memory.
If the small integer too much, will occupy too much memory, also can affect performance, so Uncle Guido default -5~256 for small integer.
So, how is a large integer initialized?
In order to reduce the number of malloc and free when creating large integer objects, Python will make a list of dozens of large integer objects at a time, called a block, and then use a block_list pointer to point to the list of all objects already occupied. When this block is exhausted, then malloc from the next block.
So, the problem comes again, for example, if the memory in the second block is not finished, the first block already has objects to do? Wouldn't it be a waste of
In Python, there is a free_list pointer to all available object memory, which is added to the Free_list list when an object is not used, and removed from Block_list.
Discover what, unexpectedly only malloc, no free, no memory Exchange system.
There should be another mechanism in python to prevent memory leaks ... I don't know how to do this right now.
Reference: Python Source code anatomy
Creation and maintenance of the [Python source] Integer object