1. Id, is, ==

In Python, you just create a data (object) will open up a space in the memory, this space is a unique identifier, it’s just like id number, identify this space is called a memory address, which is the data (object) id, then you can use the id () to obtain the data memory address.

Print (id(name)) # 1797632349264Copy the code

== is whether the values on both sides of the comparison are equal (the object’s __eq__ method is called to get the result of the comparison, and if not overwritten, the default comparison is still memory addresses), and is is whether the memory addresses on both sides of the comparison are equal. If the memory addresses are equal, then these two sides are actually pointing to the same memory address.

>>> a = 1000
>>> b = 1000
>>> print(a is b)
False
>>> print(a == b)
True
>>>
Copy the code

The above shows that a and B have the same value, but not the same memory address. We can say that if the memory address is the same, then the value must be the same, but if the value is the same, the memory address may not be the same.

2. Code blocks

Python programs are constructed from blocks of code. A block is the text of a Python program that is executed as a unit.

Block of code: a module, a function, a class, a file, etc., is a block of code.

As an interactive approach, each command entered is a block of code.

What is interactive mode? Enter the Python interpreter in CMD, and each line of code is a block of code. For example:

>>> a = 1000
>>> b = 1000
Copy the code

For two functions in a file, they are also two different blocks of code:

def fun1():
    pass
def fun2():
    pass
Copy the code

2.1. Code block caching mechanism

Prerequisite: Within the same block of code.

When Python executes a command that initializes an object from the same block of code, it checks to see if its value already exists, and if it does, it is reused. In other words, when executing the same code block, when encountering a command to initialize an object, it will store the initialized variable and its value in a dictionary. When encountering a new variable, it will first query the record in the dictionary. If the same record exists, it will reuse the previous value in the dictionary.

For objects: int (float), STR, bool

Specific details of the object :(understand)

Int (float): Any number is reused under the same code block.

Bool :True and False exist in dictionaries as 1,0, and are reused.

STR: Almost all strings are cacheable, as specified below (understand!). :

1. The string obtained by non-multiplication satisfies the caching mechanism of code block:

's # @! # e * s # e; #*ewq' print(s1 is s2) # TrueCopy the code

2. The string obtained by multiplication can be divided into two cases:

2.1. When the multiplier is 1, any string satisfies the caching mechanism of the code block:

B1 = '* * * & ^ % $# @ 5847395 qq0743895 + (()) ((& _' * 1 a1 = 'zhang @ 5847395 qq0743895 * & ^ % $# ((& _ + (())' * 1 print # (b1) a1 is TrueCopy the code

2.2. When multiplier >=2: Contains only uppercase and lowercase letters, digits, and underscores, and the total length is <=20, satisfying the caching mechanism of code block:

s1 = 'san_' * 5
s2 = 'san_' * 5
print(s1 is s2)  # True
Copy the code

Advantages: can improve some string, integer processing, in time and space performance; Need the same value of string, integer, directly from the ‘dictionary’ reuse, avoid frequent creation and destruction, improve efficiency, save memory.

3. Small data pools

Prerequisite: In a different block of code.

Python automatically caches integers ranging from -5 to 256. When you assign an integer to a variable, you don’t recreate the object. Instead, you use an already created cache object.

Python creates a copy of a regular string in the string resident pool, and when you assign the string to a variable, you don’t recreate the object, but instead use the object created in the string resident pool.

For objects: int (float), STR, bool

Specific details of the object :(understand)

Int: We all know that for integers, the small datapool ranges from -5 to 256. If multiple variables refer to the same number (within this range), they all refer to the same memory address in memory.

>>> a = 100
>>> b = 100
>>> c = 100
>>> print(id(a),id(b),id(c))
2748060816848 2748060816848 2748060816848
Copy the code

What about the specification for strings?

STR: string should be discussed from the following general directions (understand!) :

1. The length of the string is 0 or 1. By default, the string is hosted in a small data pool.

>>> a = '@'
>>> b = '@'
>>> print(a is b)
True
>>> s1 = ''
>>> s2 = ''
>>> print(s1 is s2)
True
Copy the code

2. If the length of the string is greater than 1 and contains only uppercase and lowercase letters, digits, and underscores (_), the string resides by default.

>>> a = '@'
>>> b = '@'
>>> print(a is b)
True
>>> s1 = ''
>>> s2 = ''
>>> print(s1 is s2)
True
Copy the code

3. The string obtained by multiplication can be divided into two cases.

3.1. When the multiplier is 1, any string satisfies the caching mechanism of the code block

3.2. When multiplier >=2:

Contains only uppercase and lowercase letters, digits, and underscores (_). The total length is <=20.

>>> a = 'a2_' * 4
>>> b = 'a2_' * 4
>>> print(a is b)
True
>>> a = 'a2_@' * 4
>>> b = 'a2_@' * 4
>>> print(a is b)
False
Copy the code

4. Specify dwell.

By specifying resident, you can specify any string to be added to a small datapool that creates only one object in memory, and multiple variables refer to that string.

from sys import intern a = intern('hello! @'*20) b = intern('hello! @'*20) print(a is b)Copy the code

When the above string rules are met, the concept of a small data pool is met.

Bool is True, False, and no matter how many variables you create that point to True and False, only one of them will exist in memory.

Advantages: It can improve the time and space performance of some string and integer processing tasks. When you need the same value of string, integer, directly from the pool to use, avoid frequent creation and destruction, improve efficiency, save memory.