question about True values

"Martin v. Löwis" martin at v.loewis.de
Wed Oct 25 17:14:23 EDT 2006


John Coleman schrieb:
> Yes - it just seems that there isn't a principled reason for implicitly
> converting 3 to 3.0 in 3.0 == 3 but not implicitly converting "cat" to
> boolean in "cat" == true.

Sure there is: equality should be transitive. So while we have
3 == 3L == 3.0 == 3.0+0j
and can therefore imply that 3 == 3.0+0j, we should not get
5 == True == ["foo"]
and therefore imply that 5 == ["foo"].

The phenomenon really exists only for numeric types (that two
values of different types still represent the same "thing"),
so it's not surprising that this doesn't readily extend to
other types.

In Python, there are only very few similar cases: byte strings
and unicode strings sometimes compare equal (but, since 2.5,
don't compare unequal very often); lists and tuples could be
considered to have equal values, but don't actually do compare
equal.

Regards,
Martin



More information about the Python-list mailing list