“"和“是“无"和"==没有" [英] 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.
推荐答案
一个类可以自由实现 比较选择的方式,然后 可以选择与 没有任何意义(实际上是 说得通;如果有人告诉你 从实现 从头开始,你还能怎么做 将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屋!