Fully understand python's ' = = ' and ' is '

Source: Internet
Author: User

' = = ' compares the values of two objects

' Is ' compares the memory address (ID) of two objects

Below we focus on understanding ' is '. For this, we need to know: Small integer object pool, large integer object pool, and intern mechanism

Small integer pool: Python pre-Creates a small integer cache pool--[-5~256], regardless of how many objects are created, point to the same address, in order to avoid small integers frequently requesting and freeing memory.

>>> a=3>>> b=3 is btrue1+2is3  True>>> a=1.0>>> b=1.0 is bfalse

Large integer Object pool: Python provides an extensible memory space, also known as a universal integer object pool, who needs to use it, eliminating the need for memory. This space is a pyintblock structure that connects a string of memory (block) with a one-way list, which is maintained by Block_list, and each block maintains an array of integer objects (Objects) for storing the cached integer object. That is, a large integer that is in one memory (block) is the same object.

>>> def func (): ...     A=10.1...     b=10.1      ... return  is b ..... >>> func () True

Assignments of A and B are obtained by the same universal integer pool entry.

Intern mechanism: There is a interned in the string type of Python, which is a dictionary of the string objects that are used to ensure that these strings are unique in memory, and that strings of the same value use the same object.

However, only strings made of letters, numbers, and underscores are processed intern, and strings with other characters do not.

>>> a='ABC' >>> b='abc' is Btrue>>> a='ab c'>>> b='ab c'  is Bfalse

In addition, Python's other data types such as dictionaries (dict), lists, collections (set), etc., create different objects

>>> a=[1,2,3]>>> b=[1,2,3]   is bfalse
>>> a={' a ': 1}
>>> b={' a ': 1}
>>> A is B
False
>>>

To add one, the following is the case because the previous [4,5,6] was recycled and the cache was used in the new [

>>> ID ([1,2,3]) = = ID ([4,5,6]) True> >> ID ([1,2,3])36200264L>>> ID ([4, 5,6])36200264L

Fully understand python's ' = = ' 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.