Python source code analysis Reading Notes: Chapter 4-list object

Source: Internet
Author: User
Chapter 4-list object

List object definition:
Typedef struct {
Pyobject_var_head
Pyobject ** ob_item;
Py_ssize_t allocated;
} Pylistobject;
The list object is a variable-length object, so there is a variable-length object header.
The ob_item array is a real storage container used to store pyobject object pointers.
Ob_size indicates the list length.
Allocated indicates the number of buckets allocated to the list.

Create a list in two steps. 1. Create the list object itself. 2. allocate memory for the ob_item.
The destruction of the list is also divided into two steps. 1. Reclaim the ob_item memory. 2. Destroy the list object itself.
Such an object creation and destruction solution serves the object pool (free_lists.
In the list creation stage, Python checks whether there are cached objects in free_lists. If yes, it is taken directly from free_lists. If no, the list object memory is allocated from the stack.
When the list is destroyed, if the number of cached lists (num_free_lists) is smaller than the maximum number of caches (maxfreelists), the list object is cached to free_lists for backup. If num_free_lists is exceeded, the object memory is directly released.

Set the element operation to list [I] = obj.

Insert element operation: essentially the packaging of the function ins1. The key operation of the ins1 function is to adjust the list length through list_resize (described below) and then determine the insertion point. Because the index of the python list can be negative (that is, the index of the end element is-1), when the index value is less than 0, you must add the length to get the index of the C array. Next, move the element after the inserted point backward and write the object at the inserted point. From this we can see that list is the concept of arrays in C.
List_resize function: int list_resize (pylistobject * Self, py_ssize_t newsize)
If allocated/2 <= newsize <= allocated, set ob_size to newsize. If not, use the following solution to realloc memory: new_allocated = (newsize> 3) + (newsize <9? 3: 6 );
I don't quite understand why I used this solution. Please advise.

Delete element operation: essentially the packaging of function app1. The key operation of the app1 function is to first locate the location of the first object, and then merge the two segments before and after the deleted vertex through the list_ass_slice function.

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.