![]() The argument “a” is the string object containing ‘abc’ and the argument “i” is the index requested: 0 in our case. Here is the code of the function “string_item” which is called when we request a character from a string. Instead of creating a new string containing ‘a’, the pointer at the offset 97 of the “characters” array is returned. We also find the “characters” array at use when a string’s item is requested like in the following Python script: Let’s create a new small string containing the character ‘c’. If (size = 1 & (op = characters) != NULL) The “characters” array is used during that process to check if the string already exists and returns the pointer to the string object. The same string object previously created is returned so both variables are pointing to the same string object. What happens when a different variable is assigned to the same string ‘a’? The variable “s2” is pointing to this string object. This new string object is also referenced in the array characters at the offset 97 because value of ‘a’ is 97 in ASCII. The key is a pointer to the string object and the value is the same pointer. The string object containing ‘a’ is added to the dictionary “interned”. ![]() Let’s see what happens when a new small string is assigned to a variable in your Python script. We will see later how the array “characters” is used. The array “characters” is also used to reference the strings of length 1 byte: i.e. The global variable “interned” is a dictionary referencing those small strings. Small strings are strings of size 0 or 1 byte. There is a neat feature where small strings are shared between variables. ob_sval will be of size: size + 1Įach time a new string is used, a new string object is allocated. Returns: Python string object with ob_sval = 'abc'Īllocate string object + size for 'abc'. The internal C function “PyString_FromString” is called and the pseudo code looks like this: What happens when you assign a new string to a variable like this one? “ob_sstate” indicates if the string object is in the interned dictionary which we are going to see later. If you are wondering where “ob_size is defined”, take a look at PyObject_VAR_HEAD in object.h. The initial size of “ob_sval” is 1 byte and ob_sval = 0. “ob_sval” contains the string of size “ob_size”. “ob_shash” is the hash of the string if calculated. This article describes how string objects are managed by Python internally and how string search is done.Ī string object in Python is represented internally by the structure PyStringObject.
0 Comments
Leave a Reply. |