dscdtc dscdtc - 2 months ago 12
Python Question

Why this if return True in Python, Thank you ~

>>> if '' is not None:
... print'23333'

I think (not None) is True and ('') is False so why it running print???
Thak you very much for your answer.


is and is not test for object identity, i.e., will test if '' and None are the same object, which they are not, so the test returns True in your case.

From the Python documentation:

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.

To put it another way, although '' and None have the same "truthiness", that is they both evaluate to False if you do bool(None) or bool(''), they to do not refer to the same object.