question regarding Guido's main article
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Thu Jun 3 06:02:43 EDT 2004
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
More information about the Python-list
mailing list