if __name__ == 'main':

Facundo Batista facundo at taniquetil.com.ar
Tue Mar 20 18:00:39 CET 2007

gtb wrote:

> I often see the following 'if' construct in python code. What does
> this idiom accomplish? What happens if this is not main? How did I get
> here if it is not main?
> ...
> if __name__ == 'main':
>    myQuest('myQuest').Run()

This idiom is for executing the code if you're running the .py directly,
or doing nothing if you're just "import"ing the module. 

Take note, that when you import a module, __name__ gets the module name.


.   Facundo
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/

More information about the Python-list mailing list