|
The Python Book
|
|
is and '=='
From blog.lerner.co.il/why-you-should-almost-never-use-is-in-python
a="beta"
b=a
id(a)
3072868832L
id(b)
3072868832L
Is the content of a and b the same? Yes.
a==b
True
Are a and b pointing to the same object?
a is b
True
id(a)==id(b)
True
So it's safer to use '==', but for example for comparing to None, it's more readable and faster when writing :
if x is None:
print("x is None!")
This works because the None object is a singleton.
| |