integer and string compare, is that correct?

Chris Rebert clp2 at rebertia.com
Sun Jan 10 07:46:03 EST 2010


On Sun, Jan 10, 2010 at 4:26 AM, Hellmut Weber <mail at hellmutweber.de> wrote:
> Hi,
> 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
>>>> n + max
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>>
>
>
> Section 5.9 Comparison describes this.
>
> Can someone give me examples of use cases

The behavior of disparate types being comparable is deprecated and has
been removed in Python 3.0+; don't rely upon it. (The ordering used is
arbitrary but consistent)
IIRC, the feature existed in prior versions to make lists containing
mixed types sortable; this was found to be not all that useful and to
hide what are quite arguably errors.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list