Python ' = = ' and ' is ' and the story behind them

Source: Internet
Author: User

Summary

The comparison judgment logic is used frequently in the code, in python commonly used ' = = ' and is to make the comparative judgment.

    • = = : The double equals sign is used to compare the values in the memory cells pointed to by the variable is equal, it only cares about the value, does not care about the memory address of the value, that is, it can be two different memory address values equal.
    • is: It is used to compare two variables is not pointing to the same memory unit, although it can also compare values, but it is more concerned about the memory address is the same, of course, the same value of memory address is the same.
About integers
 #   According to logic, the following code is normal  >>> a = 1 >>> B = 1>>> a == btrue  >>> a is   Btrue  >>> ID (a)  1570522768>>> ID (b)  1570522768#   Below is the moment of subversion  >>> a = 1000>>> B = 1000>>> a ==< Span style= "color: #000000;" > Btrue  >>> a is   Bfalse  >>> ID (a)  81183344>>> ID (b)  81183376 

Yes, two variables of the same value, the memory address is not the same. Of course, the precondition for this phenomenon is to execute with the Python command line, rather than using an editor such as Pycharm. The root cause is the Python interpreter problem, which involves the garbage collection mechanism of Python. The reason for this is because of something called a pool of small integer objects .

In order to optimize the speed, Python will store the data between [ -5, the sum] in a small integer pool, if the program uses the data in the small integer pool, it will not open up new memory space to create, but point to the same data in the object pool, that is, there are n variables equal to 1, Then the memory address of these n variables will point to the 1 position in the small integer pool. Small integer pools are used to avoid the frequent application of integers and the destruction of memory space. Small integer pools are built well in advance and are not garbage collected.

When the data exceeds the small integer pool, that is, the range to the large integer object pool , each time the system will request a new memory to store the data, the ' is ' does not equal to ' = = ' phenomenon will not exist.

In Pycharm, every run is loaded into memory for all code, which is a whole, and there is no such phenomenon.

About strings
#Let 's get a normal one .>>> A ='qwe'>>> B ='qwe'>>> A = =btrue>>> A isbtrue>>>ID (a)81797024>>>ID (b)81797024#feel no change, then add a little bit longer>>> A ='Q'* 20>>> B ='Q'* 20>>> A isbtrue>>> A = =btrue#It's not the same on a long point.>>> B ='Q'* 21>>> A ='Q'* 21>>> A isBfalse>>> A = =btrue>>>ID (a)81811696>>>ID (b)81811600

Cause: Python's intern mechanism .

A simple understanding is a bit like the meaning of caching, when you need to use the same string (variable assignment), directly from the cache instead of recreating, so as to avoid frequent creation and destruction, improve efficiency, save memory. The disadvantage is stitching strings, affecting performance on string modifications. Because it is immutable, the modification of a string is not a inplace operation, but a new object. This is the concatenation of the string is not recommended to use the ' + ' method, but recommend the Join function, the Join function is to calculate the length of all strings, and then copy each, and only create an object once. Each ' + ' method creates a new object. The intern mechanism is not used when the string length exceeds .

Not all strings will use the intern mechanism. Contains only underscores , letters (including case), and numeric strings are intern. Spaces and some special characters are not included. In other words, if the string contains spaces and some other special symbols (with the exception of underscores), Python does not apply the intern mechanism, but instead opens up new memory spaces to store them.

#Note that this seemingly reasonable string intern>>>'AB'+'C'  is 'ABC'         #here the string, ' ab ' + ' C ' is evaluated at complie time and is replaced by ' ABC 'True>>> N1 ='AB'>>> N2 ='ABC'>>> N1 +'C'  isN2#N1 + ' C ' is stitching in run-time, resulting in not being automatically internFalse>>> N1 +'C'  is 'ABC'False>>> N1 +'C'=='ABC'True>>> N1 +'C'==n2true

Python ' = = ' and ' is ' and the story behind them

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.