'20' <= 100

Gerhard Häring gh at ghaering.de
Thu May 1 18:18:40 EDT 2003


HP wrote:
> I have a statement somewhere in the code that
> boils down to comparing a string object with an integer.
> i.e.  var1 <= var2
> where var1 = '20' and var2 = 100
> 
> The code runs ok but the above comparison always return 0 !!
> 
> It took me quite an effort to finally pin it down.

I'd prefer Python to raise an exception if you compare a number to 
something non-numeric.

> Is it the programmer's responsibility to check the
> type of var1 and var2 before using <=   ???

Apparently :-( You can use asserts if you want to catch such errors:

assert type(t1) is int

I tend to have quite some

assert obj is not None

in my code, too.

Combined with unit tests, you have better chances that bugs like yours 
are caught earlier.

-- Gerhard





More information about the Python-list mailing list