Python Tricks--- Object Comparisons:“is” vs “==”

简介: Python Tricks--- Object Comparisons:“is” vs “==”

Python Tricks— Object Comparisons:“is” vs “==”
When I was a kid, our neighbors had two twin cats. They looked seemingly identical–the same charcoal fur and the same piercing green eyes. Some personality quirks aside, you couldn’t tell them apart just from looking at them. But of course, they were two different cats, two separate beings, even though they looked exactly the same.

That brings me to the difference in meaning between equal and identical. And this difference is crucial to understanding how Python’s is and == comparison operators behave.

The == operator compares by checking for equality

The is operator, however, compares identities: if we compared our cats with the is operator, we’d get “these are two different cats” as an answer.

But before I get all tangled up in this ball-of-twine cat analogy, let’s take a look at some real Python code.

First, we’ll create a new list object and name it a, and then define another variable(b) that points to the same list object:

In [2]: a = [1, 2, 3]

In [3]: b = a

Let’s inspect these two variables. We can see that they point to identical-looking lists:

In [4]: a
Out[4]: [1, 2, 3]

In [5]: b
Out[5]: [1, 2, 3]

Because the two list objects look the same, we’ll get the expected result when we compare them for equality by using the == operator:

In [6]: a == b
Out[6]: True

However, that doesn’t tell us whether a and b are actually pointing to the same object. Of course, we know they are because we assigned them earlier, but suppose we didn’t know- how might we find out?

The answer is to compare both variables with the is operator. This confirms that both variables are in fact pointing to one list object:

In [7]: a is b
Out[7]: True

Let’s see what happens when we create an identical copy of our list object. We can do that by calling list() on the existing list to create a copy we’ll name c:

In [8]: c = list(a)

Again you’ll see that the new list we just created looks identical to the list object pointed to by a and b:

In [9]: c
Out[9]: [1, 2, 3]

Now this is where it gets interesting. Let’s compare our list copy c with the initial list a using the == operator. What answer do you expect to see?

In [10]: a == c
Out[10]: True

Okay, I hope this was what you expected. What this result tell us is that c and a have the same contents. They’re considered equal by Python. But are they actually pointing to the same object? Let’s find out with the is operator:

In [11]: a is c
Out[11]: False

Boom! This is where we get a different result. Python is telling us that c and a are pointing to two different objects,even though their contents might be the same.

So, to recap, let’s try and break down the difference between is and == into two short definitions:

  • An is expression evaluates to True if two vairables point to the same(identical) object
  • An == expression evaluates to True if the objects referred to by the variables are equal(have the same contents)

Just remember to think of twin cats(dogs should work, too) whenever you need to decide between using is and == in Python. If you do that, you’ll be fine.

相关文章
|
6月前
|
API C++ 开发者
PySide vs PyQt:Python GUI开发史诗级对决,谁才是王者?
PySide 和 PyQt 是 Python GUI 开发领域的两大利器,各有特色。PySide 采用 LGPL 协议,更灵活;PyQt 默认 GPL,商业使用需授权。两者背后团队实力雄厚,PySide 得到 Qt 官方支持,PyQt 由 Riverbank Computing 打造。API 设计上,PySide 简洁直观,贴近原生 Qt;PyQt 增加 Pythonic 接口,操作更高效。性能方面,两者表现优异,适合不同需求的项目开发。选择时可根据项目特点与开源要求决定。
502 20
|
2月前
|
存储 大数据 Unix
Python生成器 vs 迭代器:从内存到代码的深度解析
在Python中,处理大数据或无限序列时,迭代器与生成器可避免内存溢出。迭代器通过`__iter__`和`__next__`手动实现,控制灵活;生成器用`yield`自动实现,代码简洁、内存高效。生成器适合大文件读取、惰性计算等场景,是性能优化的关键工具。
211 2
|
7月前
|
数据采集 测试技术 C++
无headers爬虫 vs 带headers爬虫:Python性能对比
无headers爬虫 vs 带headers爬虫:Python性能对比
|
4月前
|
数据采集 API 调度
Python爬虫框架对比:Scrapy vs Requests在API调用中的应用
本文对比了 Python 中 Scrapy 与 Requests 两大爬虫框架在 API 调用中的差异,涵盖架构设计、调用模式、性能优化及适用场景,并提供实战建议,助力开发者根据项目需求选择合适工具。
|
7月前
|
索引 Python
Python错误 - 'list' object is not callable 的问题定位与解决
出现编程问题并不可怕,关键在于是否可以从中学习与成长。遇到'list' object is not callable这样的错误,我们不仅需要学会应对,更需要了解其背后的原因,避免类似的问题再次出现。记住,Python的强大功能和灵活性同时也意味着我们需要对其理解更准确,才能更好的使用它。
846 70
|
数据采集 缓存 Java
Python vs Java:爬虫任务中的效率比较
Python vs Java:爬虫任务中的效率比较
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
Python Web框架比较:Django vs Flask vs Pyramid
187 1
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
Python Web框架比较:Django vs Flask vs Pyramid
204 4
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
【10月更文挑战第6天】本文比较了Python中三个最受欢迎的Web框架:Django、Flask和Pyramid。Django功能全面,适合快速开发;Flask灵活轻量,易于上手;Pyramid介于两者之间,兼顾灵活性和可扩展性。文章分析了各框架的优缺点,帮助开发者根据项目需求和个人偏好做出合适的选择。
281 4
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
【10月更文挑战第10天】本文比较了Python中三个最受欢迎的Web框架:Django、Flask和Pyramid。Django以功能全面、文档完善著称,适合快速开发;Flask轻量灵活,易于上手;Pyramid介于两者之间,兼顾灵活性和安全性。选择框架时需考虑项目需求和个人偏好。
217 1

推荐镜像

更多