Implementation of the [Python source] String object

Source: Internet
Author: User

Let's go with the problem, the same problem with the implementation of the integer object:

>>> a='ABC' >>> b='abc' is Btrue>>> c='ABC' *10>>> d='ABC'   is cfalse

Why? In the implementation of integer objects, treats small integers with small integer object pools, treats large integer pairs to request memory, and the experiment of string objects is like this???

NO

First look at the definition of the string object:

struct {pyobject_var_headlong  Ob_shash; int ob_sstate; Char ob_sval[1];} Pystringobject;

which

Ob_size in Pyobject_var_head store string actual length

Ob_shash is used to cache the actual hash value of the string object

Ob_sstate flag whether the object is handled by the intern mechanism

Ob_sval points to a length of ob_size+1 bytes of memory, ob_sval[ob_size+1] must be ' + '

The creation of a character object is to calculate the length of the string, request a memory, copy the string in memcpy, and then create the object, and all the strings will create the object. (The code is not posted ...) )

The important thing is the intern mechanism , what is this mechanism?

To put it bluntly, the intern mechanism is to create a shorter string object in a dictionary called interned to see if there are strings of the same string object, and if so, add 1 to the ob_refcnt of the object stored in the dictionary, And then destroy the newly created object, so the above scenario will appear a is B? True

In addition to the intern mechanism, the string object has a character buffer pool similar to a small integer object, which is actually pointing to this object with an array-like thing (characters array), and for a string with only one character, the first time it is created, the following actions are performed:

1. Create an Object

2. Perform intern operation on it

3. Putting objects into the character buffer pool

The next time you create this character object, you will first see if the object is present in the character buffer pool, and if so, return the buffer object. Unlike small integer objects, small integer objects are created at the beginning of the Python interpreter initialization, and the objects that the string buffer pool points to are not created until they are used.

Resources:

Python Source code anatomy

Python String Objects Implementation

Implementation of the [Python source] String 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.