Monday, May 27, 2013

The danger of simple surface syntax hiding a complicated model beneath

Getting back into the python pool, I am often bitten by nuisance features, most of them having to do with 'readable' syntax... or just syntax at all.
For example, writing a gcd method today, I wanted to say:
while b is not 0:
Which has the rather readable quality of saying exactly what I would say if I were to explain what I wanted in English, and the subtle disadvantage that it relies on object identity rather than numerical equivalence. Then I had the unfortunate wakeup call that fairly small numbers are long in python, and that the difference of two longs is long, and that these upcast variable types are not downcast to int when they come into range again. Coming from a lisp world where numbers don't play these games, and in fact
 (type-of (- 10 10))
answers bit, this was a bit of a shock. So
1L - 1L = 0L
and
 0L is not 0
is true, which is really not what I meant! I had to read the error, saying something about division by long zero, which didn't seem clear at all to me, and figure out where in the 'simple' number handling system this problem was popping up. Good times.

No comments: