__main__ : What is this?
mccredie at gmail.com
Sat Oct 20 07:45:50 CEST 2007
> En Fri, 19 Oct 2007 21:26:22 -0300, Matimus <mccredie at gmail.com> escribió:
> > The common pattern:
> > if __name__ == "__main__":
> > # do stuff
> > IMHO better written:
> > if "__main__" == __name__:
> > # do stuff
> I'm intrigued why do you feel the second alternative is better.
> Which is your native language? In English (and Spanish, and many others
> but still not in the majority) the usual ordering is "subject-verb-object"
> or SVO, which matches the first alternative: "If the name is __main__, do
> As all the languages I know (not so many!) are SVO, I can't think of any
> equivalent of the second form [that I could say it's better than the first]
English is my native language (and only, I'm American :|). The reason
I like the second version better is simply that:
variable == literal
can easily be mis-written as
variable = literal
I suppose that isn't a huge issue in Python, since most of the time
comparisons happen within if and for statements. Even if it is within
a functions parameters it will raise a SyntaxError exception. There is
still the case where one might write something like this:
a = b == 'c'
or, as I prefer:
a = 'c' == b
It is just habit from writing so much C code that way. In C the
reasoning is that if you have mistyped it, you will catch the issue at
compile time instead of runtime (which is potentially much more
difficult to debug). I'm used to seeing that pattern. In terms of
natural language I do agree with you. It really is just my _humble_
opinion. I can't make a huge argument for it in Python. To be honest,
I didn't give it much thought before I wrote it. I am simply used to
doing it that way, and being irked whenever I see it written the other
way in C or C++ (and perhaps unjustifiably Python).
More information about the Python-list