question regarding Guido's main article

Discussion in 'Python' started by Christopher Baus, Jun 3, 2004.

  1. 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:

    > python
    >>> execfile("myscript.py")


    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...

    >>> execfile("myscript.py")
    >>> foobar = "foo and a bar"
    >>> main(foobar)


    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
    http://www.baus.net/
    Tahoe, Wine, and Linux. What more could you ask for?
    Christopher Baus, Jun 3, 2004
    #1
    1. Advertising

  2. Christopher Baus wrote:
    > 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:
    >
    >>python
    >>
    >>>>execfile("myscript.py")

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


    But then __name__ *was* '__main__'.

    > What I expect was that __name__ would be something other than __main__ and
    > I would be put back at the prompt for instance...
    >
    >
    >>>>execfile("myscript.py")
    >>>>foobar = "foo and a bar"
    >>>>main(foobar)


    For this, you have to use the import statement:

    >>>import myscript
    >>>foobar = "foo and a bar"
    >>>myscript.main(foobar).


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

    HTH
    Bruno
    Bruno Desthuilliers, Jun 3, 2004
    #2
    1. Advertising

  3. > I implemented a script using the form described in the article. The then
    > did:
    >
    >> python
    >>>> execfile("myscript.py")

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


    Because execfile(), without the two additional parameters, executes your
    script in the context of the caller, of the __main__ program.

    > What I expect was that __name__ would be something other than __main__ and
    > I would be put back at the prompt for instance...
    >
    >>>> execfile("myscript.py")
    >>>> foobar = "foo and a bar"
    >>>> main(foobar)


    What you need is:

    import myscript
    foobar = "foo and a bar"
    myscript.main(foobar)

    --
    дамјан

    Weird enough for government work.
    =?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLR, Jun 10, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John M
    Replies:
    1
    Views:
    1,115
    Kumar Reddi
    May 29, 2005
  2. Hal Styli
    Replies:
    14
    Views:
    1,615
    Old Wolf
    Jan 20, 2004
  3. Frederick Ding

    int main() or int main(void)?

    Frederick Ding, Dec 3, 2005, in forum: C Programming
    Replies:
    10
    Views:
    641
  4. john_re
    Replies:
    0
    Views:
    262
    john_re
    Apr 4, 2009
  5. Brian Newtz

    Impersonation question regarding a microsoft article

    Brian Newtz, Dec 23, 2003, in forum: ASP .Net Security
    Replies:
    2
    Views:
    127
    Brian Newtz
    Dec 24, 2003
Loading...

Share This Page