Monday, February 1, 2010

More Python Woes

So I had a bug: I had a method on a class that had lost its indent (I love that academic/APA style allows one to avoid assigning credit or blame).

The error I got was that the interpreter couldn't find the attribute. Attribute? It's not an attribute, it's a method. I knew it wasn't a method signature problem, as I had checked the name, and if you have a mismatch in arguments, then you get a different error.

However, the method was clearly a member of a class, but the "compiler" didn't notice that it referred to self, even though it was a top-level function (not physically in the class). Why doesn't the compilation routine notice these kinds of errors? I really do like compilers, because I make mistakes. I'll take this chance to tell a favorite anecdote.

I spent a great deal of time, as an undergraduate, in the UPL (Undergraduate Projects Lab) in the CS department at the University of Wisconsin, run by Professor Barton Miller. One day, there was a heated debate about preferred debuggers between some acolyte programmers. The acolytes asked the guru-in-residence (one of three great programmers I know), whom we'll call "Mitch" (because that's his name), "Mitch, what's the best debugger?" Mitch replied, "I don't write bugs. I got over that."

Now, you're ready to call this either braggadocio or just plain bull, but there's a codicil anecdote. A few years later, I was talking to Mitch, and he was in a bad mood. I asked him what was wrong, and he replied, "I wrote a bug."

Anyway, I write bugs. I'm a decent programmer, perhaps even good, certainly not in the stratosphere. There are three programmers I put on a pedestal, but many, many better and more diligent than I am. So I like things that catch my bugs. Python does not do that.

You might argue, at this point, that it's a minor detail, and that if I were a better programmer, or more versed with/experienced in python, I wouldn't have had this problem (which admittedly, once I figured it out, was easy to solve). You are right on the latter count, but I would argue that it's a major detail. Yes, a detail, but an important one.

In the OO world, we deal with classes, objects, methods, and attributes. These are the quanta of our product. For a language to not notice that a method does not belong to a class, well, that's a static analysis issue, and it should be done. One of the lovely things about java is that it does an excellent job with static analysis (for the most part) at compile-time. You cannot have a method that doesn't belong to a class (love the valid use of double negatives). If you want a method to operate on the class, rather than an object, you have to specify that explicitly, with the static keyword. Arguments and variables are checked; you can't access an object variable in a class (static) method.

No comments:

Post a Comment