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