I recently came across this syntax, I am unaware of the difference.
I would appreciate it if someone could tell me the difference.
I recently came across this syntax, I am unaware of the difference.
I would appreciate it if someone could tell me the difference.
The answer is explained here.
To quote:
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?).
Practically-speaking, there is not much difference since custom comparison operators are rare. But you should use is None
as a general rule.
is
v. ==
. –
Dola is None
is a bit (~50%) faster than == None
:) –
Beneficiary is
is, basically, integer comparison while ==
is not only resolving references but comparing values which may have mismatching types. –
Hedelman ==
should be used in all cases except when either speed matters or you really need to know if you are referring to the same object. ==
is much simpler and easier to understand. It doesn't leave the reader wondering "why did he use is
instead of ==
". –
Overdevelop is None
unless you have a good reason to do == None
", but it was precisely the "what if someone wants to implement a None
-like object" case which made me start wondering if actually maybe == None
is The Right Way(tm)... –
Uppermost foo.bar
throws a TypeError: NoneType has no attribute "bar"
and you're left having to step backwards through the logic and figure out where the None
that ended up assigned to foo
could have come from. I thought the other day that a magic None
object which isn't a singleton but instead carries annotations about where it came from would help that, but that would break is None
checks. Basically, when we say is None
, we're marrying ourselves to the language implementation/design choice that None
is a singleton. –
Uppermost None
in Python (or Options in most code that has options) would actually be better designed if it used DebugAnnotationMonad<Option<T>>
instead, where the DebugAnnotationMonad
is a hypothetical mostly-transparent wrapper holding metadata that would be useful for debugging (which a language could even choose to optimize out, like how release builds can strip out assert statements in most languages). –
Uppermost class Foo:
def __eq__(self, other):
return True
foo = Foo()
print(foo == None)
# True
print(foo is None)
# False
In this case, they are the same. None
is a singleton object (there only ever exists one None
).
is
checks to see if the object is the same object, while ==
just checks if they are equivalent.
For example:
p = [1]
q = [1]
p is q # False because they are not the same actual object
p == q # True because they are equivalent
But since there is only one None
, they will always be the same, and is
will return True.
p = None
q = None
p is q # True because they are both pointing to the same "None"
x == None
may evaluate to True
even if x
is not None
but an instance of some class with its own custom equality operator. –
Dg It depends on what you are comparing to None. Some classes have custom comparison methods that treat == None
differently from is None
.
In particular the output of a == None
does not even have to be boolean !! - a frequent cause of bugs.
For a specific example take a numpy array where the ==
comparison is implemented elementwise:
import numpy as np
a = np.zeros(3) # now a is array([0., 0., 0.])
a == None #compares elementwise, outputs array([False, False, False]), i.e. not boolean!!!
a is None #compares object to object, outputs False
If you use numpy,
if np.zeros(3) == None: pass
will give you an error when numpy does elementwise comparison.
np.array(None) is None
: False
. –
Twosided © 2022 - 2024 — McMap. All rights reserved.
==
andis
in python? – Coltis
vs==
, or about the nature of what exactlyNone
is and how the behaviour differs in either context (the latter is why I ended up here). Based on the vagueness and lack of OP responses... I'm surprised this has so many upvotes. I mean... cmon... the question is not even written in the actual question... – Leprechaun