question regarding Guido's main article

  • Thread starter Christopher Baus
  • Start date
C

Christopher Baus

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:

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

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

Bruno Desthuilliers

Christopher said:
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:



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

For this, you have to use the import statement:

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

HTH
Bruno
 
G

Guest

I implemented a script using the form described in the article. The then
did:



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

What you need is:

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

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top