Home  >  Article  >  Backend Development  >  Description of the location of integer object storage in Python

Description of the location of integer object storage in Python

高洛峰
高洛峰Original
2017-03-16 17:09:161151browse

In Python Integer Objects are stored in different locations. Some are pre-allocated memory and are always stored in memory, while others are in use. When opening up space.


The reason for saying this, you can look at the following code:

a = 5
b = 5
a is b # True
a = 500
b = 500
a is b # False

It can be seen from the above code that the integer type 5 is has always existed, but the integer type 500 has not always existed.


So which integersare pre-allocated memory addresses?

a, b, c = 0, 0, 0
i = 0
while a is b:
    i += 1
    a, b = int(str(i)), int(str(i))
else:
    print(i) # 打印 257

As we know from the above, non-negative integers less than or equal to 256 (2**8) are always stored (that is to say, their memory addresses are allocated in advance and do not need to be allocated later)

a = -1
b = -1
a is b # False

And negative numbers will not be pre-allocated.


The above is the detailed content of Description of the location of integer object storage in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn