“是无"和“无"有什么区别?和“==无" [英] What is the difference between "is None" and "== None"
问题描述
我最近遇到了这种语法,我不知道其中的区别.
I recently came across this syntax, I am unaware of the difference.
如果有人能告诉我其中的区别,我将不胜感激.
I would appreciate it if someone could tell me the difference.
推荐答案
答案已经解释 这里.
引用:
一个类可以自由实现比较它选择的任何方式,它可以选择对比没有任何意思(实际上说得通;如果有人告诉你从实现 None 对象从头开始,你还能怎么做将 True 与自身进行比较?).
A class is free to implement comparison any way it chooses, and it can choose to make comparison against None mean something (which actually makes sense; if someone told you to implement the None object from scratch, how else would you get it to compare True against itself?).
实际上,由于自定义比较运算符很少见,因此没有太大区别.但是您应该使用 is None
作为一般规则.
Practically-speaking, there is not much difference since custom comparison operators are rare. But you should use is None
as a general rule.
这篇关于“是无"和“无"有什么区别?和“==无"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!