osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Odd truth result with in and ==


On 21/11/2018 19:18, Python wrote:
> $ python3
> Python 3.5.2 (default, Nov 23 2017, 16:37:01)
> [GCC 5.4.0 20160609] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> 1 in [1,2,3] == True
> False
>>>> 1 in ([1,2,3] == True)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: argument of type 'bool' is not iterable
>>>> (1 in [1,2,3]) == True
> True
>
> How is the first not equivalent to either one of the second or third?
> My expectation is it should produce the same result as the second.  It
> *seems* like Python is ignoring the '1 in' part and just giving the
> result for '[1,2,3] == True'...  Is this just a bug?
>
I've followed this thread with interest, as I do with threads like this, 
and learnt a useful detail about Python.

But the following I found unexpected. (Python 3.6 on a Windows 7 64 bit box)

 >>> if []: print("Truthy")
...
 >>> if [1,2,3]: print("Truthy")
...
Truthy
 >>>

from which I concluded [] is Falsey and [1,2,3] is Truthy and the above 
if statements work as expected.

but,

 >>> [1,2,3] == True
False
 >>>

is unexpected as to my mind as [1,2,3] is 'Truthy' and True has ultimate 
'Truthiness'.

Any ideas? Is there an implicit 'casting' taking place and if so is this 
documented somewhere?

I interpret the above comparison as

 >>> bool([1,2,3]) == bool(True)
True
 >>>

Thanks everyone.