integer and string compare, is that correct?

Marco Salden marco.salden at gmail.com
Mon Jan 11 02:50:21 EST 2010


On Jan 10, 1:26 pm, Hellmut Weber <m... 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
>
> TIA
>
> Hellmut
>
> --
> Dr. Hellmut Weber         m... at hellmutweber.de
> Degenfeldstraße 2         tel   +49-89-3081172
> D-80803 München-Schwabing mobil +49-172-8450321
> please: No DOCs, no PPTs. why: tinyurl.com/cbgq

I would say you want to compare semantically an integer value with an
integer value so why not:
IDLE 1.1.3
>>> max = '5'
>>> n = 5
>>> n==(int(max))
True
>>>
?
(in Python 2.4...)

Regards,
Marco



More information about the Python-list mailing list