Location of integer object storage in Python

Source: Internet
Author: User
The Python integer object is stored in a different location, some of it is pre-allocated memory, it has been stored in memory, and others, when used to open up space.


To say the reason for this sentence, you can look at the following code:

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

As the above code shows, the integer 5 is always there, and the integer 500 is not always there.


So what integers are pre-allocated memory addresses?

A, b, c = 0, 0, 0i = 0while A is b:    i + = 1    A, b = Int (str (i)), int (str (i)) Else: print    (i) # printing 257

As is known, non-negative integers less than or equal to 256 (2**8) are stored all the time (i.e. their memory addresses are pre-opened and do not need to be redistributed later)

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

and negative numbers are not pre-opened.

  • Related Article

    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.