python basis of variables and data storage caching mechanism resides small data pool

A: variable storage cache mechanism

In the same file (module), the caching mechanism variables stored (in charge of only python3.x version), then

- For Number (int bool float complex)

1. For integer, the same value within the range of positive infinity consistent -5 ~ id

2. For floating point number, the same as a non-negative value within a range consistent id

3. For Boolean values ​​are the same circumstances, id consistent

4. The complex is not the same identifier id (Real + Imaginary in this configuration)


(1) int -5 ~ + infinity within the range
var1 = 18 is
var2 = 18 is
Print (ID (var1), ID (var2)) # same
var1 = -99
var2 = -99
Print (ID (var1), ID (var2) ) are not the same #
var1 = -6
var2 = -6
Print (ID (var1), ID (var2)) are not the same #

(2) float nonnegative
var1 = 3.15
var2 = 3.15
Print (ID (var1), ID (var2) ) # same
var1 = -6.88
var2 = -6.88
Print (ID (var1), ID (var2)) # is not the same as

(3) bool worth the same address in the same boolean value or
var1 = False
var2 = False
Print (ID ( var1), id (var2)) # same as

(4) never the same as in the case of real + imaginary number
var1 =. 5 + 3j
var2 =. 5 + 3j
Print (ID (var1), id (var2)) # is not the same as
'' except the case where the imaginary number, exceptions' ''
var1 = 8J
var2 = 8J
print (id (var1), id (var2)) # same


- for the type of container portion
concerned 5. string, the string value in the same case, id consistent
6. lists, tuples, dictionaries, set no matter what, id identification is different (but the same id identifies an empty tuple)


(1) str 
var1 = "you"
var2 = "you"
Print (id (var1), id (var2)) # same

(2) empty tuple
var1 = ()
var2 = ()
Print (ID (var1), ID (var2)) # same as

(3) the remaining address data of all container types are not the same
var1 = (l, 2,3)
var2 = (1,2, . 3)
Print (ID (var1), ID (var2)) # is not the same

var1 = []
var2 = []
Print (ID (var1), ID (var2)) # is not the same

var1 = { 'A':. 1}
var2 {= 'A':. 1}
Print (ID (var1), ID (var2)) is not the same as #

II: data resides small data pool

Different files (modules), the portion of the data resides small data pool (responsible only for python3.x version)

python advance creates an integer ranging from -5 to 256 in memory resides in the memory of an area.

If the files are different (block) of two variables, and having the same range of values,

So id the same.

Only for small data pool: int, string, bool, and an empty Ganso (), None effective keywords

For strings:

Length (1) of the string is 0 or 1, the default data resides small pool

 

 

(2) the length of the string> 1, and contains only lowercase letters, numbers, underline, the default data resides small pool

 

(3) multiplying the resulting string, two cases.

1) multiplier is 1:

Whatever the string * 1, by default resides small data pool

2) the multiplier is greater than 1:

Multiplier is greater than 1, comprising only the numbers, letters, underline is cached, but the string length is not greater than 20

 

 

Specify the Park

from sys import intern

a = intern ( '* ^^ & blind monk 1234' * 10)

b = intern ( '* ^^ & blind monk 1234' * 10)

print(a is b)

# You can specify any string of small data added to the pool, no matter how many variable declarations, as long as this value is the same, all point to the same address space

 

 

* Whether caching mechanism or mechanisms for small-resident data pool, are designed to save memory space, improve code efficiency

Guess you like

Origin blog.csdn.net/qq_24036403/article/details/91344782