question regarding Guido's main article

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Baus

    question regarding Guido's main article

    Hi,

    I'm new to Python and am learning the languge for writing test scripts for
    C++.

    I just finished reading this:



    article. Coming from C++ I am a bit confused about the relationship of
    the interpreter to main. I think I understand the __name__ variable, it
    just doesn't work as expected.

    I implemented a script using the form described in the article. The then
    did:
    [color=blue]
    > python[color=green][color=darkred]
    >>> execfile("myscr ipt.py")[/color][/color][/color]

    This immediately called my main function, which should have only been
    called if __name__ == "__main__".

    What I expect was that __name__ would be something other than __main__ and
    I would be put back at the prompt for instance...
    [color=blue][color=green][color=darkred]
    >>> execfile("myscr ipt.py")
    >>> foobar = "foo and a bar"
    >>> main(foobar)[/color][/color][/color]

    That way I could pass any arguments to main or do processing before
    calling main. The article mentions calling main from the interactive
    prompt, I just don't see how to do this.

    Thanks for your help...

    --
    Christopher Baus

    Tahoe, Wine, and Linux. What more could you ask for?




  • Bruno Desthuilliers

    #2
    Re: question regarding Guido's main article

    Christopher Baus wrote:[color=blue]
    > Hi,
    >
    > I'm new to Python and am learning the languge for writing test scripts for
    > C++.
    >
    > I just finished reading this:
    >
    > http://www.artima.com/weblogs/viewpost.jsp?thread=4829
    >
    > article. Coming from C++ I am a bit confused about the relationship of
    > the interpreter to main. I think I understand the __name__ variable, it
    > just doesn't work as expected.
    >
    > I implemented a script using the form described in the article. The then
    > did:
    >[color=green]
    >>python
    >>[color=darkred]
    >>>>execfile("m yscript.py")[/color][/color]
    >
    >
    > This immediately called my main function, which should have only been
    > called if __name__ == "__main__".[/color]

    But then __name__ *was* '__main__'.
    [color=blue]
    > What I expect was that __name__ would be something other than __main__ and
    > I would be put back at the prompt for instance...
    >
    >[color=green][color=darkred]
    >>>>execfile("m yscript.py")
    >>>>foobar = "foo and a bar"
    >>>>main(foobar )[/color][/color][/color]

    For this, you have to use the import statement:
    [color=blue][color=green][color=darkred]
    >>>import myscript
    >>>foobar = "foo and a bar"
    >>>myscript.mai n(foobar).[/color][/color][/color]

    Note that 'myscript.py' must be in the sys.path for import to work
    correctly.

    HTH
    Bruno

    Comment

    Working...