[Chicago] Why is '' > 0 True?

Chris Foresman foresmac at gmail.com
Tue Dec 8 17:59:26 EST 2015


See also: https://docs.python.org/2/reference/expressions.html#not-in <https://docs.python.org/2/reference/expressions.html#not-in>

“The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects need not have the same type. If both are numbers, they are converted to a common type. Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrarily.”

Also, for your ‘’ > str(0) example:

“Strings are compared lexicographically using the numeric equivalents (the result of the built-in function ord()) of their characters. Unicode and 8-bit strings are fully interoperable in this behavior.”

In other words, an empty string is never “greater than” any non-empty string, including ‘0’.



Chris Foresman
chris at chrisforesman.com



> On Dec 8, 2015, at 2:16 PM, Adam Forsyth <adam at adamforsyth.net> wrote:
> 
> 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/ <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 <mailto: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 <mailto: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 <mailto:Chicago at python.org>
> https://mail.python.org/mailman/listinfo/chicago <https://mail.python.org/mailman/listinfo/chicago>
> 
> 
> 
> _______________________________________________
> Chicago mailing list
> Chicago at python.org <mailto:Chicago at python.org>
> https://mail.python.org/mailman/listinfo/chicago <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/8839b078/attachment.html>


More information about the Chicago mailing list