Huh?!?!? What is it that I'm seeing here?

Discussion in 'Python' started by William Trenker, Sep 14, 2003.

  1. The above code reads something like this: if the name of the current module is __main__, then invoke the run method with the parameter to run() being a list of the remaining arguments on the command line, excluding the argument that is the name of the python script to be executed.

    The name of a module is set by Python to the value __main__ when the module is invoked from the command line. So if I have a command line like this:

    python arg1 arg2 arg3

    then the code for will be loaded and run. Since was loaded from the command line it will be given the module name __main__ and so the if statement will succeed. The run method will be invoked with argv[1:] expanded like this:

    run(['arg1', 'arg2', 'arg3'])

    (argv[0] is the value '' from the above command line example.)

    Hope that helps,
    William Trenker, Sep 14, 2003
    1. Advertisements

  2. William Trenker

    Don Bruder Guest

    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__':

    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...
    Don Bruder, Sep 14, 2003
    1. Advertisements

  3. William Trenker

    Tobiah Guest

    performing properly (albeit somewhat inefficiently, since I haven't done
    This is used to check whether the script file is being
    run directly as a python script (as opposed to being
    imported as a module).

    One use of this is to put unit tests in each module filed.
    Then you can just do "python" to run the code
    under the if statement. When you import the module, the
    test code will not be run.

    Tobiah, Sep 14, 2003
  4. William Trenker

    Don Bruder Guest

    This is used to check whether the script file is being
    run directly as a python script (as opposed to being
    imported as a module).

    One use of this is to put unit tests in each module filed.
    Then you can just do "python" to run the code
    under the if statement. When you import the module, the
    test code will not be run.


    BEAUTIFUL! Exactly the kind of information I was hoping for!

    Looks like that means I can safely "lose" all occurrences, since once I
    finish the port, it'll be a monolithic program.

    Thanks Tobiah!
    Don Bruder, Sep 14, 2003
  5. William Trenker

    John Roth Guest

    What's confusing you is that Python executes the module when it's
    first run (or imported.) That means that any code outside of a def
    is executed directly, including those last two lines that have
    you puzzled.

    "__main__" is the name of the module that is executed from the
    command line, regardless of what its name was on disk. What
    that means is that the if statement will succeed if the module is
    run directly from the command line, but will fail if it is imported
    from some other module.


    John Roth
    John Roth, Sep 14, 2003
  6. William Trenker

    John Hunter Guest

    Don> if __name__ == '__main__': run(argv[1:])

    You can safely ignore these in your conversion project.

    if __name__ == '__main__'

    This means if the text file "" holding the module code is
    run as the main file, as in
    if the file has an executable bit set


    This means "call the function run, passing command line arguments as

    Basically, the code at the bottom allows any python module to save the
    same purpose as the C/C++ function 'main'. Once you have converted
    the python module, and implemented the function "run" in your C/C++
    code, you would do something like

    int main( int argc , char** argv ) {

    John Hunter
    John Hunter, Sep 15, 2003
  7. William Trenker

    Gary Feldman Guest

    What you may want to do is to take the code executed by these idioms and
    port them into your unit testing system. And I highly recommend unit
    testing for any porting project.

    Gary Feldman, Sep 15, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.