The difference between python is and ==

(*-*)浩
Release: 2019-08-10 15:38:27
Original
4600 people have browsed it

The difference between pythonis and ==:

The difference between python is and ==

##is is used to determine two variable references Whether the objects are the same, == is used to determine whether the values ​​of reference variables are equal.

a is b is equivalent to id(a)==id(b), id() can obtain the memory address of the object. (Recommended learning:

Python video tutorial)

If a=10;b=a; then the memory addresses of a and b are the same at this time;

But when a =[1,2,3]; When b=a[:], although the values ​​of a and b are the same, the memory addresses are different.

If you define a=10, b=10 at this time, and then compare a is b, you will find that the returned result is True. This is because a small shaping pool will be created in Python with a range of [ -5,256] to open up memory space for these integers. When integers within this range are defined in the code, the memory address will not be reallocated.

And I tested it in Pycharm:

#coding=utf-8
a=100000000000;
b=100000000000;
print a is b
Copy after login

The result:

 True
Copy after login
After checking the information, I found out: For performance reasons, Python will not repeatedly create immutable objects in the same code block, but will directly reference existing objects as long as they have the same value.

For more Python related technical articles, please visit the

Python Tutorial column to learn!

The above is the detailed content of The difference between python is and ==. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!