= = and is

Source: Internet
Author: User

The objects in Python contain three elements: ID, type, value
ID: Used to uniquely identify an object;

Type: Identifies the types of objects;

Value is the values of the object.

= = is to determine whether the value of the object is equal, that is, Value,is is to determine whether the object is itself, that is, ID.

1 ' ABC ' 2 ' ABC ' 3 Print  is b 4 5 # Print Result: True,true

The above A and B points should not be the same object, why A is B printing true! After searching, the original "temporarily stored in Python and reused short string as an optimization, in fact, there is only one string in memory for a, a, a two shared reference", so A is B printing is true, the following to verify:

1 ' a '*2552'a'*2553print is b 4 5 # Printing Result: True, False

The above C and D are all long strings equal to 255 a, all pointing to different objects, printing A is B to false, and verifying that "temporarily store and reuse short strings within Python as an optimization, in fact, there is only one string in memory for a, a, two shared reference" sentence. Next, you can look at the ID of a,b,c,d:

Print ID (a), id (b) Print ID (c), id (d) # Print Result: 34220632, 34220632 #          33885504, 33885224

As you can see, A and B point to different objects, but because Python is temporarily storing and reusing short strings for optimization, the IDs are the same, that is, that A is B is true, and that C and D point to objects that are long strings, so the IDs conform to C and D pointing to different objects, false.

= = and is

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.