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