Common Python Idioms

Gabriel Genellina gagsl-py at yahoo.com.ar
Thu Dec 7 22:16:34 EST 2006


At Thursday 7/12/2006 19:45, Steven D'Aprano wrote:

> > I'm surprized that none of these pages mentions the incompatible type
> > comparison gotcha:
> >
> >>>> 5 < "4"
> > True
> >
> > I'm sure this has bitten many folks, particularly (ex) Perl'ers.
>
>Why is this a gotcha?
>
>I can't speak for others, but except for sorting, I've never been tempted
>to compare ints to strings like that, but thinking about the code I've
>written, I can't think of any bad consequences that would have happened if
>I had.
>
>I'm not saying that there are never bad consequences for comparing
>incompatible types, but I'm questioning that it is a gotcha, let alone a
>common gotcha. What do others think? Ever been bitten by 5 < "4"?

Yes. By example, on a web application. If you forget to convert the 
form or query values to their correct type, you could mix things 
inadvertidely like that.
Of course, not exactly as written, but

def test(self, value):
     if value>self.value: self.value=value

(when self.value is an int, but value comes as a string)

As always, once you know that, you make sure it wont happen again...


-- 
Gabriel Genellina
Softlab SRL 

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! 
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar



More information about the Python-list mailing list