My Python learning notes: = = and is

Source: Internet
Author: User

Three basic elements contained in Python: ID (Identity), type (data type), value

    • = = is used to compare value (value) of two objects for equality,
    • IS is used to compare the IDs (identity) of two objects for equality
= = Example:

    • X, Y, z three objects are all [three-in-one], so true in the case of = =
    • X and y have the same ID, so x is Y is True, and X and Z have different IDs, so X is Z false

Is example: First, Integer object

For integer objects, Python caches a number of frequently used integer objects and saves them to a list of called Lists small_ints , where any reference to these integer objects is no longer recreated in the entire life cycle of python, but rather directly referencing objects in the cache. Python places the small objects in the range [-5, 256] that are likely to be used frequently, but takes them from within the scope of the little small_ints integers, and no longer creates new objects temporarily.

1. When an integer object is within the interval [-5,256], the is result is true (the ID value of a and B is the same)

2. When an integer object is not within the interval [-5,256], the is result is false (the ID value of a, B differs)

3, read the above two examples, we continue to look at the following code:

If, according to the 2nd conclusion, the 257 is not within the interval [-5,256], then two is the result should be false, but why is an is B true?

In order to understand this problem, first of all we must first understanding the code block problem of the program. Python programs consist of blocks of code, which are executed as a minimal basic unit of the program. A module file, a function body, a class, and a single line of code in an interactive command are called a block of code. In the above code, consists of two blocks of code, c = 257 as a block of code, the function test as another block of code. Inside Python in order to further improve performance, any integer object created in a block of code, if there is a value with the same object in the code block, then directly reference, otherwise create a new object out. Python is based on performance considerations, but for non-mutable objects, objects in the same code block, only objects of the same value, are not created repeatedly, but directly refer to objects that already exist. Therefore, not only the integer object, but also the string object follows the same principle. So it's taken for granted that instead of being a is b True in the c a same block of code, two objects are created inside Python that are all 257.

Note: As shown in, running the following code in Pycharm, the result is true, because objects of the same value are not created repeatedly in the same block of code, but instead directly refer to objects that already exist

Second, character and string object

When a, B is a string object, Python has the intern mechanism, which means that when a new string object is created, if it already has a string object with the same value as it, the reference to that object is returned directly, and the newly created string object is not returned. Include only alphanumeric underlined strings, and Python uses the intern mechanism for them. (So the is operation returns True when the string contains only alphanumeric underlines, and false if there are other characters)

Note: When a, B is a single character object, the is operation returns true

Iii. Other types of objects

When a, B is a tuple, list,dict, and set type, the is operation result is false

My Python learning notes: = = and is

Related Article

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.