my first ever Python bug
2020-06-13 20:50Years and years ago, when Perl was still the dominant scripting language, with Python and Ruby nipping at its heels, and CPAN was where you would look for cool libraries, I wrote my first Python program. I don't recall what it did, whether it was meant to do something useful or just do something like "10 9 1 blastoff" as a test, but I do know that it looped based on a command line parameter. And that it didn't work, running nigh-forever when it had a parameter of '20' and should stop after 20 times. It took me like 20 minutes to figure out what was wrong.
I can replicate the basic problem:
And the behavior still replicates -- if you run it in Python 2. In Python 3, you'll get
Because sys.argv is a list of strings. Thing is, Perl is a very accommodating language; the equivalent code in Perl will just work, as I well knew. Or rather, Perl has separate comparison operators for numeric and string values, so this code, using '<', would quietly try to convert sys.argv[1] into a number for comparison with 'count'. (If you wanted lexicographic comparison, you would use 'count lt sys.argv[1]', except of course it would be more like '$count lt $argv[1]' or something; anyway, there the value of $count would be converted to a string for comparison.)
I remember being very put out. Though I did go write a 'real' script in Python, and showed it to my boss, who was able to read and understand it with no Python knowledge. Go go executable pseudocode.
These days, on the rare occasions I poke at Perl, I'm more likely to be annoyed by the lack of a REPL, or easy ways to print nested data structures, or the quirky way function arguments are handled. Perl still does some things better ('use strict;') but I don't miss it.
But dang, that bug.
I can replicate the basic problem:
import sys count = 0 while count < sys.argv[1]: print(count) count += 1 else: print("Blastoff!")
And the behavior still replicates -- if you run it in Python 2. In Python 3, you'll get
TypeError: '<' not supported between instances of 'int' and 'str'
Because sys.argv is a list of strings. Thing is, Perl is a very accommodating language; the equivalent code in Perl will just work, as I well knew. Or rather, Perl has separate comparison operators for numeric and string values, so this code, using '<', would quietly try to convert sys.argv[1] into a number for comparison with 'count'. (If you wanted lexicographic comparison, you would use 'count lt sys.argv[1]', except of course it would be more like '$count lt $argv[1]' or something; anyway, there the value of $count would be converted to a string for comparison.)
I remember being very put out. Though I did go write a 'real' script in Python, and showed it to my boss, who was able to read and understand it with no Python knowledge. Go go executable pseudocode.
These days, on the rare occasions I poke at Perl, I'm more likely to be annoyed by the lack of a REPL, or easy ways to print nested data structures, or the quirky way function arguments are handled. Perl still does some things better ('use strict;') but I don't miss it.
But dang, that bug.