Search code examples
pythonintegercomparatorsquish

Integer comparison in Python


I came across something odd. I'm kind of new with Python so sorry if this is basics.

a = 12
b = int(24/3)
x = (a < b)

From the debugger: I have:

a (int) = 12
b (int) = 8
x (bool) = True

So it seems that 8 > 12 in this situation, I'm clueless here, any explanation?

PS: x = a < b does the same (without brackets)

EDIT I'm using squish (automatic tests), and it seems that's the issue as I asked some colleagues to test the same snipet in squish and it did the same.


Solution

  • This is a well-known behaviour, though not exactly intuitive, behaviour of Squish. Your int call doesn't use the Python int function but rather invokes the int constructor for constructing an integer which can be passed to methods in the application under test (setWidth or so). I.e. Squish overrides the meaning of int.

    You can use

    import __builtin__
    a = 12
    b = __builtin__.int(24/3)
    x = (a < b)
    

    to enforce getting the Python int.