Comparison of strings and integer

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Tue Jul 25 13:52:21 EDT 2000


Franz GEIGER wrote in comp.lang.python:
> Still not very experienced in programming Python I recently compared
> accidentially a string variable with an integer variable. Python did it his
> way, which was not my way: I expected a conversion like Perl does or a
> runtime error like REBOL does. Is there any way to prevent Python from doing
> so?
> 
> Python: print "1" > 1 --> 1
> 
> Perl: print not ("1" > 1); --> 1
> 
> REBOL:  print "1" > 1 -->
> ** Script Error: Expected one of: string! - not: integer!.
> ** Where: print "1" > 1

The best way to prevent this is "don't do that then!" :-).

Python's docs say that sorting order for incompatible types is
arbitrary but consistent. Or so I seem to remember, can't find it at
the moment.

One way Python's behavior can be useful is for sorting a list; at least
equal values will end up grouped together. It would be irritating if
heterogenous lists couldn't be sorted at all.

What should it do? Complain every time an integer is compared with
another type? What about floats? What about complex numbers? What
about class instances with a __cmp__ method? If the rule is complicated,
it becomes harder to remember.

-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
  7:51pm  up 1 day,  8:43,  6 users,  load average: 0.73, 0.91, 0.92



More information about the Python-list mailing list