gogoWebsite

Python object pool

Updated to 14 days ago

Python object pool

In python, everything is an object

The return value of the id method is the memory address of the object.

is operator, if the reference is the same object, it returns true, x is y is similar to id(x) == id(y)

Small integer object pool

[-5, 256] These small integers are defined in an integer object pool. When referring to small integers, objects in the integer object pool will be automatically referenced, so these small integers will not be created repeatedly. When multiple variables point to the same small integer, they are essentially pointing to the same object.

String

String objects are immutable objects. Python has an intern mechanism. Simply put, it is to maintain a dictionary. This dictionary maintains the address (value) of the string (key) and its string object. Every time a string object is created, it will be compared with this dictionary. If it is not, it will be created. If it is repeated, it will be referenced with a pointer. The intern mechanism processes string lengths less than or equal to 20 and are only composed of underlined numbers, and is created only once.

Floating point type

float type can be considered that each assignment is to create an object because there are many float values

Tuples

Tuple is an immutable object, maybe because the search overhead is too high and there is no intern mechanism to implement it. In fact, it is just an array. This array is the same as the array in C. It allocates memory space every time it is created.

Constant pool

Constants with the same value that appear in the same compilation unit (PyFunctionObject) will only appear in the constant pool. Constants include large integers, floating point numbers, and strings.

 

Interactive interpreter test results

>>> a = -6
>>> b = -6
>>> a is b
False
>>> a = -5
>>> b = -5
>>> a is b
True
>>> a = 256
>>> b = 256
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
False

 

Execute source code file results

a = -5
b = -5
print(a is b)  # True
a = -6
b = -6
print(a is b)  # False
a = 256
b = 256
print(a is b)  # True
a = 257
b = 257
print(a is b)  # True

 

Differences between line-by-line explanation and overall explanation

Why do the above two identical codes have different execution results? This takes into account the problem of the compilation unit.

The "compilation unit" of CPython's code is a function - each function is compiled separately, and the result is a PyFunctionObject object with various information such as bytecode, constant pool, etc. Python's top-level code is also regarded as a function.

In CPython's interactive interpreter, Python will use it as a compilation unit to compile to bytecode and interpret execution for each input line of code; if the input code has not yet formed a complete unit, such as a function declaration or class declaration, wait until the input of the complete unit is obtained and then processed as a compilation unit.

A large integer in a code block is the same object. In the following code, a and b are in a code block, while c and d have their own code blocks, so they are not equal.

a = 1000
b = 1000
print(a is b)  # True
def f1():
    c = 1000
    return c
def f2():
    d = 1000
    return d
print(f1() is f2())  # False