Just today I came accross about this strange behaviour of the in
operator in Python (using Python 3.6.3 to be more specific).
>>> ':' in '4:2'
True
>>> ':' in '4:2' != True
True
>>> (':' in '4:2') != True
False
>>> ':' in ('4:2' != True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
As it can be seen, ':' in '4:2'
is True
. Everything normal here. But the strange behaviour appears in the second line:
>>> ':' in '4:2' != True
True
':' in '4:2'
is True
, which != True
and results in… True
? If we group by hand to ensure the precedence:
>>> (':' in '4:2') != True
False
Results in False
. True != True
is False
as expected. Then, how did we get the True
before?:
>>> ':' in ('4:2' != True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
What's going on?