Location of integer object storage in Python

Source: Internet
Author: User
In Python, the storage location of integer objects is different in the storage location of Python integer objects, some of which are pre-allocated memory, which is always stored in the memory, while others, the space is opened up during use.


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

A = 5b = 5a is B # Truea = 500b = 500a is B # False

The code above shows that integer 5 always exists, while integer 500 never exists.


Which integers are pre-allocated memory addresses?

A, B, c = 0, 0, 0i = 0 while a is B: I + = 1 a, B = int (str (I )), int (str (I) else: print (I) # prints 257

As we know, non-negative integers smaller than or equal to 256 (2 ** 8) are stored all the time (that is, their memory addresses are pre-opened and do not need to be distributed later)

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

Negative numbers are not pre-opened.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.