别院牧志知识库 别院牧志知识库
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 辨析

    • Python 中 exec()和 eval()的区别
    • Ptyhon 中使用 is None 而不是 ==None 问题
      • 参考链接
  • Sockets编程

  • Django

  • stackoverflow

  • Flask

  • 全栈之路

  • 面试

  • 代码片段

  • 异步编程

  • 😎Awesome资源

  • PEP

  • Python工匠系列

  • 高阶知识点

  • Python 学习资源待整理
  • 设计模式

  • 好“艹蛋”的 Python 呀!
  • FIFO | 待学清单📝
  • pip 安装及使用
  • 数据分析

  • 源码阅读计划

  • OOP

  • 关于 python 中的 setup.py
  • 并行分布式框架 Celery
  • 七种武器,让你的代码提高可维护性
  • 使用 pdb 调试 Python 代码
  • 每周一个 Python 标准库
  • 🐍Python
  • 辨析
佚名
2020-08-10
目录

Ptyhon 中使用 is None 而不是 ==None 问题

首先需要区分 Python 中is和==的区别:

  • python - Is there a difference between "==" and "is"? - StackOverflow (opens new window)
  • python - Is there any difference between "foo is None" and "foo == None"? - StackOverflow (opens new window)

is比较在两个对象是否相同时使用(即通过比较在内存中的标识符(id(obj))),而==通过调用__eq__()方法比较两个的值是否相等。

看下面的 回答 (opens new window) :

is is generally preferred when comparing arbitrary objects to singletons like None because it is faster and more predictable. is always compares by object identity, whereas what == will do depends on the exact type of the operands and even on their ordering.

This recommendation is supported by PEP 8, which explicitly states that "comparisons to singletons like None should always be done with is or is not, never the equality operators."

通常,在将任意对象与None之类的单个对象进行比较时首选is,因为它更快且更可预测。 is总是根据对象标识进行比较,而==的作用取决于运算对象的确切类型,甚至取决于它们的顺序。 PEP 8 支持此建议,该声明明确指出“与单例的比较(如None,应该始终使用is或 not进行比较,永远不要使用相等运算符进行比较)”。 在工作效率上,is的效率明显高于==:

>>> a = timeit.timeit("1 is None", number=10000000) # 0.6208912429997326

>>> b = timeit.timeit("1 == None", number=10000000) # 0.9341017190004095

>>> a /b
0.6125248705781231
1
2
3
4
5
6

当然,在数值上来看差距不是很大,但是量变引起质变。

# 参考链接

Python: "is None" vs "==None" | Jared Grubb (opens new window)

python - What is the difference between " is None " and " ==None " - StackOverflow (opens new window)

需要注意的是:除了None之外,别的代码的地方最好都不要使用is比较!

警告

Why you should almost never use "is" in Python — Reuven Lerner (opens new window)

编辑 (opens new window)
#Python#编程
上次更新: 2024-07-15, 08:03:22
Python 中 exec()和 eval()的区别
Python 中的 Socket 编程

← Python 中 exec()和 eval()的区别 Python 中的 Socket 编程→

最近更新
01
提升沟通亲和力的实用策略
03-26
02
工作
07-15
03
如何选房子
06-25
更多文章>
Theme by Vdoing | Copyright © 2019-2025 IMOYAO | 别院牧志
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式