473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

if __name__ == 'main':

gtb
Hi,

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?

Thanks,

gtb

==============================================

if __name__ == 'main':
myQuest('myQuest').Run()

Mar 20 '07 #1
4 3485
On 2007-03-20, gtb <go*************@hotmail.comwrote:
I often see the following 'if' construct in python code. What does
this idiom accomplish?
It checks to see if the file is being run as the "main"
program, and does something if that is so.
What happens if this is not main?
Nothing.
How did I get here if it is not main?
By importing the file.
if __name__ == 'main':
myQuest('myQuest').Run()

--
Grant Edwards grante Yow! Thank god!!... It's
at HENNY YOUNGMAN!!
visi.com
Mar 20 '07 #2
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.

Regards,

--
.. Facundo
..
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/
Mar 20 '07 #3
On Mar 20, 11:49 am, "gtb" <goodTweetieB...@hotmail.comwrote:
Hi,

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?
A quick example demonstrates the usage:

C:\code>type temp.py
print "Module name is",__name__

if __name__ == "__main__":
print "I was not imported"
else:
print "I was imported"

C:\code>python temp.py
Module name is __main__
I was not imported

C:\code>python
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>import temp
Module name is temp
I was imported
Mar 20 '07 #4
gtb
On Mar 20, 12:13 pm, "Patrick Down" <patrick.d...@gmail.comwrote:
On Mar 20, 11:49 am, "gtb" <goodTweetieB...@hotmail.comwrote:
Hi,
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?

A quick example demonstrates the usage:

C:\code>type temp.py

print "Module name is",__name__

if __name__ == "__main__":
print "I was not imported"
else:
print "I was imported"

C:\code>python temp.py
Module name is __main__
I was not imported

C:\code>python
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.>>import temp

Module name is temp
I was imported

Thanks, all! Makes great sense.
Teas all 'round the canteen now,

gtb

Mar 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Eli Stevens \(WG.c\) | last post by:
I have a question about proper Python style when it comes to having a main function in a module. I'm fairly new to Python - a few months of very-part-time tinkering (lots'o'Java at work, shrug);...
1
by: Christopher Baus | last post by:
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...
19
by: Arthur | last post by:
Did I hallucinate something about __name__ becoming read-write? Not in alpha2. Can't find the reference to this I thought I read - that it was concluded to be necessary in connection with...
5
by: Ed Leafe | last post by:
I'm working on creating a generic runtime engine for the Dabo framework. Right now I'm focusing on Windows, since many of our potential users are running on that platform. I've got py2exe and Inno...
4
by: Rhamphoryncus | last post by:
First a bit about myself. I've been programming in python several years now, and I've got several more years before that with C. I've got a lot of interest in the more theoretical stuff (language...
19
by: beliavsky | last post by:
I think I read a suggestion somewhere to wrap the code where a Python script starts in a main() function, so one has def main(): print "hi" main() instead of
6
by: fatwallet961 | last post by:
is the main function in python is exact compare to Java main method? all execution start in main which may takes arguments? like the follow example script - def main(argv): ...
3
by: bg_ie | last post by:
Hi, Lets say I have the following class - class MyClass: def __init__(self): print (__name__.split(".")) if __name__ == '__main__': MyClassName = "MyClass"
8
by: gtb | last post by:
The lines if __name__ == 'main': someClass().fn() appear at the end of many examples I see. Is this to cause a .class file to be generated? The last line of the sample below has a string...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.