Decrypts the python id () function

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;
Change
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.