Hey all,
So I am starting to learn python and I ran into a weird occurrence. I had code which essentially does this:
I could run the code 5 or 6 times and the last time through it would give me a division by zero. For more details see below. My thought is that this looks like something I am familiar with in
Java -
String comparisons where you make an entity based comparison (ie str1 == str2) when that type of comparison isn't appropriate. Turns out 'is' is an entity based comparison in Python, so I changed the code to value based comparisons (in Python, == is value based if I understand correctly):
When I did that, the problem appears to have gone away. It sort of makes sense to me, in Python integers are Objects, maybe the 0 value is cached so initial tests with 'is' look ok, but then my no-ops cause enough memory to cycle that makes the cached value get flushed and now 0 isn't the same cached object that was assigned to _count and the 'is' comparison no longer works.
I just don't know if that is true or not, and I would like to get confirmation if anyone has any idea...
Some more background - I am actually
testing other conditions, and those conditions make sure the _count never increments. When the count does increment I would never get a div by 0 error. This is a single threaded application - no chance of some other
thread modifying countF or _count behind the scenes. I couldn't duplicate it by simply putting this code in a loop, but I could duplicate it quite easily by putting the application through 5 or 6 no-ops which have no effect on _count. More specifically, I am using IronPython2.4.