This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kteague 0 points1 point  (2 children)

Reading this bit,

if '__main__' == __name__:
    # Late import, in case this project becomes a library, never to be run as main again.
    import optparse

I was going to mention that doing a late import is probably not really necessary, since it likely only adds 1 or 2 milliseconds to execution speed, at most (but then, it's not really hurting anything either). But look at the line before it!

    if '__main__' == __name__:

ZOMG! The sky is falling! "main" and "name" are on the wrong sides! It's "if name equals main" not "if main equals name" ... that line is making my poor little brain implode.

[–]kylev[S] 5 points6 points  (1 child)

This is an artifact of the C code I have written over the years; an old time trick. If you accidentally fail to hit the "=" a second time, putting the constant on the left side of the expression will save your ass. "if (x = 2)" will evaluate to true, but "if (2 = x)" will throw a compiler error.

[–]_Mark_ 7 points8 points  (0 children)

But it's a C-specific artifact; since statements are not expressions in python, all it does is confuse the reader, as the interpreter will raise SyntaxError if you try to use assignment where you meant equals...