Three characteristic variables - python basis

Three elements of variables

id () variable corresponding to the position occupied memory
type () variable corresponding data type
value () value assigned to the variable value of the actual memory space open to the inside

After using variable names must follow the call to define (******)

print

x = 10
print(x)  # 获取变量的变量值 10

Determining whether the value of the variable is equal to

name1 = 'egon'
name2 = 'nick'
print(name1 == name2)  # False

Determining whether the same variable id

x = 11
y = x
z = 11
print(x == y)  # True
# True
print(x is y)  # True
# True
print(x is z)  # True,整数池的原因
# True
x = 257
z = 257

print(x is z)  # False

As can be seen from the above print messages: variable id equal, equal to a certain value, pointing to the same memory address; value equal to the variable, id not necessarily equal.

Which at the time of the first print print (x is z) would trigger the last chapter speaks of integer pool. This can be understood as a mechanism to optimize the python, the value itself is not 11, and because of our fast once again use the 11, again due to the application memory space required computer overhead, so let python x and z point to the same 11. Because the deposit is not an end, to take is the purpose of such optimization case does not affect the operation of the program.

Personal summary

In the range of values ​​of the variables -5 to 256 or disguised compare Compare id id cases such attention is required plus a small pool of thinking integer

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374625.html