Frage

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.

War es hilfreich?

Lösung 2

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.

Andere Tipps

Something is fundamentally wrong, perhaps in your debugger or another part of your code. Python is reasonably bug-free with basic arithmetic.

>>> a = 12
>>> b = 24/3
>>> a < b
False

The code works just fine. There are no bugs. As others have suggested, something is wrong with your debugger. I'd suggest editing the question to include details about the environment you are using so we can replicate the issue.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top