Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does 'is not None' serve any purpose here?

I was surprised to discover that "X in Y is not None" does not throw an exception. I can't figure out how Python actually interprets this. What value does "is not None" operate on? Does it serve any purpose here?

>>> "asdf" in {} is not None
False
>>> 
>>> "asdf" in {"asdf": 1} is not None
True
>>> 
like image 431
J. Darnell Avatar asked Dec 14 '25 03:12

J. Darnell


1 Answers

Python comparison chaining strikes again.

According to the same rules that says that 1 < 2 < 3 is equivalent to 1 < 2 and 2 < 3, these expressions:

"asdf" in {} is not None
"asdf" in {"asdf": 1} is not None

Are equivalent to:

"asdf" in {} and {} is not None
"asdf" in {"asdf": 1} and {"asdf": 1} is not None
like image 86
that other guy Avatar answered Dec 16 '25 19:12

that other guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!