开发者

What tricks do you use to avoid being tripped up by python whitespace syntax?

I'm an experienced programmer, but still a little green at python. I just got caught by an error in indentation, which cost me a significant amount of debugging time. I was wondering what experienced python programmers do to avoid creating such problems in the first place.

Here's the code (Part of a much larger program) :

class Wizvar():

    def select(self):
        self.selected = True

    def unselect(self):
     开发者_开发问答   self.selected = False

        value = None

The problem is that 'value = None' should be outdented one level. As it is, the variable gets clobbered every time the unselect method is called, rather than once only. I stared at this many times without seeing what was wrong.


Put all the class attributes (e.g. value) up at the top, right under the class Wizvar declaration (below the doc string, but above all method definitions). If you always place class attributes in the same place, you may not run into this particular error as often.

Notice that if you follow the above convention and had written:

class Wizvar():
        value = None

    def select(self):
        self.selected = True

    def unselect(self):
        self.selected = False

then Python would have raised an IndentationError:

% test.py
  File "/home/unutbu/pybin/test.py", line 7
    def select(self):
                    ^
IndentationError: unindent does not match any outer indentation level


In general: a lot of unit testing. Code reviews also help a lot.

This specific error seems like an easy one to identify since if value was supposed to be outdented once it would have been a class variable, proper unit testing would have spotted that it isn't in this case.

Tools like PyDev do a good job at finding some other common mistakes so you might want to consider those.


I don't have such problems ;) At least I have them less often than a superfluous, missing or misplaces brace or the classic:

if (foo) 
    bar();
    baz();

in language that use braces.

That being said, certain coding styles help. For example, I always list class variables at the top of the class body, so if I accidentally the indentation, I'll get an IndentationError instead of creating an unused local variable. By the way, I've always seen it like this. Consistent indentation (I'm with PEP 8 and use 4 spaces) also helps, some people use only one space for some blocks - that's really easy to overlook.

Static code analysis (like PyLint) may point such errors out, but I don't have much experience with these. As I wrote, it just works most of the time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜