Python ID () function decryption process

Source: Internet
Author: User

>>> A = 2.5
>>> B = 2.5
>>> C = B
>>> A is C
False

>>> A = 2
>>> B = 2
>>> C = B
>>> A is C
True

When I use the is function today, I print the numbers A and B are assigned 2.5 and 2 respectively. I found that:
>>> A = 2
>>> B = 2
>>> ID ()
21132060
>>> ID (B)
21132060
>>> A = 2.5
>>> B = 2.5
>>> ID ()
19622112
>>> ID (B)
29321464

When A and B are 2, the ID is the same, but 2.5 is different. This problem also occurs when the string is short, when B is assigned a very short string, their IDs are the same, but long strings do not;
Then, we can get a simple conclusion: the interpreter makes a small Optimization for int and short strings with a small value, and only allocates one object, let them have the same ID.

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.