Python's equivalent to Main calling program and subprograms

Hanif Jameel hanif_1989 at btinternet.com
Wed Dec 1 12:25:30 EST 2010


On 01/12/2010 17:08, m b wrote:
>
>
>  > >
>  > > if __name__ == "__main__":
>  > > main()
>
> What does this mean?
>
> /Mikael
>
Python will not cause the main() function to run automatically when you 
execute the script, it has to be called.

__name__ is a special variable which is set by the python interpreter 
when it runs a source file. If the source file is run directly(as 
opposed to being imported), then the value of __name__ will be set to 
__main__.

so

if __name__ == "__main__":
   main()

checks to see if the source file be being directly executed, and if it 
is, calls the main() function.





More information about the Python-list mailing list