main
Paddy
paddy3118 at netscape.net
Sat Feb 3 00:37:11 EST 2007
On Feb 3, 4:45 am, fatwallet... at yahoo.com wrote:
> is the main function in python is exact compare to Java main method?
>
> all execution start in main which may takes arguments?
>
Hi Fatwallet,
May I have some of your money?
Oh, sorry, the main function...
The main function is *not* like that of Java, if Java is ike C in
that execution starts in main().
In Python, you can create a function called main and it behaves just
like any other function.
Stick that function in a file and the python interpreter will create a
function object out of it, assigned to the name main.
Now, while directly interpreting a python file, the interpreter sets
the global name __name__ to the value '__main__'
If the file is being interpreted due to it being referenced via an
import statement, then __name__ is set to the name of the module being
imported.
You can therefore use the value of the __name__ variable to get a file
to do different things when imported versus when directly run.
Some people arrange for a function called main to be called when
directly interpreted; other don't.
Its a convention only.
Others use the trick to call test functions when directly executed,
when the file is normally imported as a module.
> and what is
> __name__
> __main__
>
> use for in terms of Java?
>
With respect, (hehe), maybe you need to indicate that you've searched
the Python documentation on __name__ and __main__?
(Hah! I did that without saying RTFM. - Oh pooh! Fiddlesticks).
- Paddy.
More information about the Python-list
mailing list