I found this story on python-ml-jp mailing list.
This is not a bug. Python behaves like this since is is not a binary operator.
>>> None is None is None
True
>>> (None is None) is None
False
>>> None is (None is None)
False
It's well known that you can write a code like
to do something if a is smaller than b and b is smaller than c.
if a < b < c:
do something
This is because < is a comparison, which is not a binary operator.
It is taken as
This behavior is documented here.
if a < b and b < c:
do something
If it was a binary operator, it would be taken as
is and in are categorized in tests. Though it doesn't seem to be clearly documented, a test behaves the same way as a comparison.
if (a < b) < c:
do something
2 comments:
Nice post.
Cheers :)
Post a Comment