[Chicago] Why is '' > 0 True?

Adam Forsyth adam at adamforsyth.net
Tue Dec 8 15:16:51 EST 2015


John is basically right, though there is some nuance as numeric types are
treated as having an empty string as their class name. See this Stack
Overflow question I answered a few years ago:

http://stackoverflow.com/questions/7969552/why-does-4-3-return-true-in-python-2/

Incompatible types are no longer orderable in Python 3.

Adam




On Tue, Dec 8, 2015 at 1:45 PM, John Cronan <kyle at pbx.org> wrote:

> IIRC, in the spec it says that objects of different types are compared by
> their class names, alphabetized. The only reason it's required to be in the
> spec at all is that hashes can have keys of any type, so you need some
> well-defined ordering.
>
> -JKC
>
> On Tue, Dec 8, 2015 at 1:40 PM, Robare, Phillip (TEKSystems) <
> proba at allstate.com> wrote:
>
>> A bug in our code has been hunted down and at its core is the fact that
>> in Python
>>
>>
>>
>> ‘’ > 0
>>
>>
>>
>> is True.  It is actually true for any string and any integer.
>>
>>
>>
>> My expectation would be that the string form of ‘>’ would coerce its
>> right argument to something comparable to its left argument.  But
>>
>>
>>
>> ‘’ > str(0)
>>
>>
>>
>> is False.
>>
>>
>>
>> Obviously we should not be trying to order mixtures of strings and
>> integers so the bug is fixed, but I am wondering why this works the way it
>> does.  Can anyone enlighten me?
>>
>>
>>
>> Phil Robare
>>
>>
>>
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> https://mail.python.org/mailman/listinfo/chicago
>>
>>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> https://mail.python.org/mailman/listinfo/chicago
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20151208/412159e7/attachment.html>


More information about the Chicago mailing list