Creation and maintenance of the [Python source] Integer object

Source: Internet
Author: User

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

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.