Python id () function introduction

Source: Internet
Author: User
This article introduces the id () function in python. For more information, see >>> a = 2.5.
>>> B = 2.5
>>> C = B
>>> A is c
False
>>> A = 2
>>> B = 2
>>> C = B
>>> A is c
True

When using the is function, print the numbers a and B are assigned 2.5 and 2 respectively, and find 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;

I have read the following articles:
Http://stackoverflow.com/questions/4293408/ids-of-immutable-types
Http://stackoverflow.com/questions/3402679/identifying-objects-why-does-the-returned-value-from-id-change
Then, we can get a simple conclusion: the interpreter made a small optimization for int and short strings with a small value, and assigned only one object to make 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.