'20' <= 100

Raymond Hettinger vze4rx4y at verizon.net
Thu May 1 18:45:47 EDT 2003


> 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.
>
> Is it the programmer's responsibility to check the
> type of var1 and var2 before using <=   ???

Short answer:  Yes.

Long answer:  Why check, just coerce to what you need.
    int(var1) <= int(var2)
    str(var1) <= str(var2)
    # Note, these can give different answers
    # You're probably looking for the first one


Raymond Hettinger






More information about the Python-list mailing list