First off, I'm a non-Python type person - I prefer a compiled language,
when the option is available, with a strong preference for C and/or C++.
(Sorry, boyz-n-girlz, I'm not interested in "You need to learn a real
language like _________" fights, or debates over the merits and/or
shortcomings of C/C++ - C/C++ is what I want the program I'm working
with to be written in, and that means that all discussion regarding
language choice is moot. End of discussion there.)
I'm in the process of trying to convert a Python script/program or
whatever the favored term is into C and/or C++, and of course, due to my
unfamiliarity with Python, I'm hitting some stuff that looks to be
idiomatic, without being able to locate a definition/description of the
idiom. Most of what I'm messing with seems pretty straightforward - it's
converting nicely into C++, and with my testing so far, appears to be
performing properly (albeit somewhat inefficiently, since I haven't done
any tweaking yet) but one particular construct has me stumped. It looks
like this:
if __name__ == '__main__':
run(argv[1:])
This (or a markedly similar construct) sits all by its lonesome self at
the bottom of several of the source files, apparently not part of any
class or declaration. It seems to be *FAIRLY* common in the group of
files that make up the program, which makes me think it may be some sort
of "preprocessor directive" type thing, similar to a C construct that
would conditionally compile in a "main()" routine if a given symbol
is/isn't defined. In all cases where this construct appears, there's a
corresponding "run()" method, which, confusingly, appears to be outside
the scope of any lexical blocks that I can recognize, so I've got no
contextual clue there...
I've paged through all the online Python docs I can find, but although I
recall seeing a reference to __name__ as some sort of special
variable/identifier, that was when I first came up against the language,
so I paid no attention at that time. Now, when I actually NEED it, I
can't LOCATE it! Can anybody help me out here? Judging by how common it
is in the files in this program, and the fact that I simply cannot
re-find the discussion of the "__name__" thing, I have to assume it's
one of those "Duh! Anybody smarter than a sheep already knows that!"
constructs that anyone with some experience with the language has
used/understands without needing more explanation.
Unfortunately, I have little-to-no experience with Python, and even less
interest in learning it - chances are so far beyond "high" that I'll
never write a single line of code in it that the number might as well be
infinite, since I have zero interest in taking the time to learn a new
language for a one-off project. I simply want a program that was
originally (however good, bad, or in-between the original decision might
have been) written in Python turned into a C/C++ program that I can work
with effectively using existing skills and knowledge. Not
knowing/understanding the function of this construct appears to be
hindering that process, so maybe somebody here can help me out?
Thanks in advance...