Object Identity comparison, is, integer object, and string object Cache

Source: Internet
Author: User

I recently started to learn python. In fact, I have been learning python for two or three years. <~), However, there has never been any "incentive .. Then the lab recently needs to learn about SWIFT Object Storage. In addition, the entire set of open-source cloud platform openstack is written in Python, so go started.

As the first learning record, the question is really difficult .. Go to the topic ..

First, the question is thrown:

Define Variables

 
1A = 102B = 103C = 100.4D = 1005E = 10.06F = 10.0

Please provide the following comparison output results

1AIsB2 #True3 4CIsD5 #True6 7EIsF8 #False

First, we need to identify the difference between is and =: Is is used to test whether two objects point to the same object, and = compares the value of the object. So

A is B

This expression is equivalent to the line expression:

ID (A) = ID (B)

Therefore, in the above questions, the "6" numeric objects we created are not actually 6, but a and B point to the same, C and D point to the same, E and F are respectively 10.0 objects. Why have we changed the six objects we should have created to four? This isSince integer objects and string objects are immutable objects, Python caches them very efficiently, which leads to the assumption that python should not create new objects when creating new objects.Let's look at two examples:

 X = 9999  Y  = 9999  S1  =  '  A string   '  S2  =  '  A string   ' #  output result  x  is   Y  #  false   S1   is   S2  #  true  

X and Y both created 9999. Why didn't they point to the same 9999 object? This is becausePython only caches simple integer data because it is considered to be used in Python applications.ProgramThese small integers are frequently used, and the range varies according to different versions.In python2.3, it is decided that strings outside the pre-defined cache string table will not be cached if there is no reference pointing to it.

In addition, for another non-mutable type tuples, its behavior is somewhat different from the integer object and string object, for example:

 
T1 =() T2=() T3= (1, 2) T4= (1, 2)#Output resultT1IsT2#TrueT3IsT4#False

therefore, it does not indicate that the same object Value in the unchangeable type has the same reference, depending on the specific integer value range, whether the tuples are empty, and so on.

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.