473,385 Members | 1,769 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,385 software developers and data experts.

newb: What is the purpose of if __name__ == "__main__":

What is the purpose of
if __name__ == "__main__":

If you have a module, does it get called automatically?

Dec 7 '06 #1
3 2274
johnny wrote:
What is the purpose of
if __name__ == "__main__":

If you have a module, does it get called automatically?
http://docs.python.org/lib/module-main.html

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Dec 7 '06 #2
johnny wrote:
What is the purpose of
if __name__ == "__main__":

If you have a module, does it get called automatically?
It is a common pattern to make one and the same source file usable as a
program and as a module. When you import some python module its
__name__ is never "__main__". When you run a python script __name__ is
always "__main__". This way you can control what shall happen when you
run the source file as a standalone program.

To put it simple: Everything in the `if __name__ == "__main__":` block
is only executed when you run the source file as a program.

Hope that helps,
--
Soni Bergraj
http://www.YouJoy.org/
Dec 7 '06 #3
"johnny" <ra*******@gmail.comwrites:
What is the purpose of
if __name__ == "__main__":
<URL:http://www.python.org/infogami-faq/tutor/tutor-what-is-if-name-main-for/>

--
\ "He that would make his own liberty secure must guard even his |
`\ enemy from oppression." -- Thomas Paine |
_o__) |
Ben Finney

Dec 7 '06 #4

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

Similar topics

2
by: marco | last post by:
Hi, first of all ; sorry for my poor english ; i'm french ... and i hope you can understand below I use python (and wxpython) on a win32 platform, to build a simple "home theater pc". I want...
4
by: Amy G | last post by:
I have a program that needs a little help. Right now the program runs in my crontab. When it runs, it sets a few variables based on a query to a MySQL database. I would like to modify it so that...
51
by: Noam Raphael | last post by:
Hello, I thought about a new Python feature. Please tell me what you think about it. Say you want to write a base class with some unimplemented methods, that subclasses must implement (or...
22
by: Qopit | last post by:
Hi there, I'm pretty new to Python and am trying to figure out how to get "will this code compile?"-like code checking. To me this is a pretty basic language/environment requirement, especially...
2
by: wen | last post by:
and, in which case, the following case will happen: if __name__!='__main__': do_sth() any help would be appreciated.
2
by: wen | last post by:
i have written some code in test.py as below: import __main__ if __name__!='__main__': print 1 print 2 when i run test.py, i got 2 on the screen.
9
by: Emanuele Aina | last post by:
I have some code which does a lot of "in" on lists containing objects with no __eq__ defined. It all goes fast until I add the __lt__() method: then I have a slowdown comparable to the one I get...
1
by: NachosRancheros | last post by:
Ok so I just started to program with Python about a week ago and I am trying to make a program that will take the path of a file and a shortcut command and save it to a text file. Eventually I want...
3
by: bsagert | last post by:
After a couple of weeks studying Python, I already have a few useful scripts, including one that downloads 1500 Yahoo stock quotes in 6 seconds. However, many things are puzzling to me. I keep on...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.