Home > Backend Development > Python Tutorial > Where integer objects are stored in Python

Where integer objects are stored in Python

高洛峰
Release: 2016-10-17 11:53:06
Original
1332 people have browsed it

The storage locations of integer objects in Python are different. Some are pre-allocated memory and are always stored in memory, while others open up space when used.


Say this For the reason, you can look at the following code:

a = 5
b = 5
a is b # True
a = 500
b = 500
a is b # False
Copy after login

From the above code, we can see that the integer type 5 has always existed, but the integer type 500 has not always existed.


So which integers have pre-allocated memory addresses? What?

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
Copy after login

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
Copy after login

And negative numbers will not be pre-opened.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template