Why I should be debugging again!

for i in range(len(my_list)):
   if some_condition:
       del my_list[i]

the range(len()) doesn’t get calculated again, so i get an IndexError for sure, the closer it gets to the end. Because in the beginning there might were 5 elements in my_list but when I delete one and at the end I want to access my_list[4] I get the IndexError, because that’s now my_list[3], or course. Looking at the code didn’t make it obvious, because “I knew it was correct” and “it is just too simple to contain an error”. I am not human, I am a programmer who knows!

That’s why I should be debugging again, I saw that right away, just put the watch on the my_list and the first del made me see the problem. That’s why I should get used to debug right away!

That made me think about my supposedly development in developing.
In 1986 or 1987 I started with Basic, I don’t think I was debugging that. There was not even a debugger for it, or was there? For my KC87 and KC 85/3 there was surely no debugger. Then, I don’t remember the year, I switched to Turbo Pascal (cooles pic). That had debugging of course. And that was just easy as changing dipers (is for me now)! F4, F5, F6 and all those key combinations for Step Over, Step Into, etc. Then in 1990 *thinking* … yes, when I was in 11th grade. I started to use C, that was strange. All those curly braces and this strange i++ construct. Hoooh, that scared me at first. Hey man I was programming graphic stuff in Pascal, I even knew how to use the mouse and all that, and now you come to me with curly braces and that stuff. But a debugger was just standard at this time, of course! Later I was doing a lot with Delphi, Assembler, C++ and some others I guess. And I was always debugging my code. Well I remember that I didn’t when I was programming multi-threaded stuff in Delphi for my diploma, of course.

So what the heck was I doing when I accepted to stop debugging when I started programming PHP (actually already with Perl, that I was doing little before)? I think it was the fascinating world of the web, that has opened to me and made me forget about the comforts of debugging and other programming tools, that I had used until then.

But fortunately there are also debuggers for PHP now and thanks to the WingIDE there is a great debugger for Python, with all the “step into”, “step over” and watch list things, even the same key combinations as I remember them from Pascal. Great. So I will start to creep out of my black whole and debug again.

5 Comments »

  1. pythohead said,

    November 7, 2005 at 7:24 pm

    winpdb is a new debugger that is quite nice:
    http://www.digitalpeers.com/pythondebugger/

  2. Jay P. said,

    November 7, 2005 at 8:41 pm

    Regarding your code, using filter() (or itertools.ifilter) would probably be a better way to do what you’re doing. Iterating over the length of the list, and removing items by index, is very bad form (in Python).

    Jay P.

  3. Wolfram said,

    November 7, 2005 at 8:46 pm

    I learned that this was really bad code, I am still in the process of catching up with what is pythonic, and that was just to illustrate the problem. thanks anyway

  4. Alex said,

    November 8, 2005 at 3:05 pm

    Here’s how I’d do it:

    my_list = [e for i, e in enumerate(my_list) if some_condition]

  5. Alex said,

    November 8, 2005 at 3:06 pm

    Oops, should be

    my_list = [e for i, e in enumerate(my_list) if not some_condition]

    I frequently need the debugger myself. :-)

RSS feed for comments on this post · TrackBack URL

Leave a Comment