473,665 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('myQues t').Run()

Mar 20 '07 #1
4 3500
On 2007-03-20, gtb <go************ *@hotmail.comwr ote:
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('myQues t').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('myQues t').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.comwr ote:
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...@g mail.comwrote:
On Mar 20, 11:49 am, "gtb" <goodTweetieB.. .@hotmail.comwr ote:
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.>>i mport 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
11442
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); my apologies if this has been hashed out before. Random Googling didn't enlighten me, so instead I'll ask here. :) Take the following strmod.py (it's silly, I know): ---- import sys import getopt
1
1274
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 from C++ I am a bit confused about the relationship of
19
1798
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 PEP318. Better get my facts straight first.... But if true that would seem to solve the main objection to:
5
2746
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 Setup running, so all that is well and good. My question concerns the ability to generically run scripts as if they were being run on an installed copy of Python. Many scripts have the following structure: if __name__ == "__main__":
4
1661
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 design, component architectures, etc). Of late my focus has been on concurrent operations (and on how to design a GUI architecture, but that's not what this post is about). I've looked at threads, and the inability to kill them easily was a...
19
28555
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
3810
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
1300
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
2315
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 parameter. When I
0
8779
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.