integer and string compare, is that correct?

Peter Otten __peter__ at web.de
Sun Jan 10 08:30:31 EST 2010


Hellmut Weber wrote:

> being a causal python user (who likes the language quite a lot)
> it took me a while to realize the following:
> 
> 
> leo at sylvester py_count $ python
> Python 2.6.3 (r263:75183, Oct 26 2009, 12:34:23)
> [GCC 4.4.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> max = '5'
>  >>> n = 5
>  >>> n >= max
> False

> Section 5.9 Comparison describes this.
> 
> Can someone give me examples of use cases

The use cases for an order that works across types like int and str are weak 
to non-existent. Implementing it was considered a mistake and has been fixed 
in Python 3:

Python 3.1.1+ (r311:74480, Nov  2 2009, 15:45:00)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 5 > "5"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() > str()

Checking for equality is still possible:

>>> 5 == "5"
False

Peter



More information about the Python-list mailing list