dscdtc - 8 months ago 34

Python Question

`>>> if '' is not None:`

... print'23333'

...

23333

I think (not None) is True and ('') is False so why it running print???

Thak you very much for your answer.

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.

Source (Stackoverflow)