Where and when I would use if( __name__ == “__main__”):


I’m Confused. pls, Clarify on this

I clearly understood the concept of above statement, but I don’t know where the situation which I can use that __name__ statement effectively. What I have understood on the statement is  python interpreter has some variables by default like that it also have a variable called __name__. During the execution of python files it will assign values to that variable. If it is an stand-alone program execution then the value of __name__ would be “__main__” if python is imported then the value of __name__ would be the file name of the python file.

#Filename: foo1.py
print “Value of __name__ Variable:  ”+__name__

if I execute the above file.
Execution: python foo1.py

Output is:

Value of __name__ Variable:  __main__

Whereas if I execute the file as module by importing (i.e.)

#Filename: foo2.py
import  foo1

print “I am foo2”

Execution: python foo2.py


Value of __name__Variable: __foo1.py
I am foo2

Now, my question is,  if(__name__ ==”__main__”): statement used only to identify whether it is main program or stand-alone program. Isn’t it? Am I correct? Pls clarify more……

Balakrishnan V.



PYGTK Simple Tasks

Hi to all,

A long ago Raji akka teached pygtk to us, also i learned pygtk with eager at the time. But after that i didn’t worked it out regularly, fortunately i had to do some simple task in pygtk, I started it from scratch finally i did one Hello World program and Simple currency converter. May be these programs are simple, but it shows i too know pygtk 😉

1. Hello World program

#!/usr/bin/env python
import pygtk
import gtk

class HelloWorld:
    def hello(self, widget, data=None):
        print "Hello World"
    def delete_event(self, widget, event, data=None):
        print "delete event occurred"
        return False
    def destroy(self, widget, data=None):
        print "Completely quit"
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)
        self.hobox = gtk.HBox(False, 0)
        self.b = gtk.Button("Hello World")
        self.b.connect("clicked", self.hello)
        self.hobox.pack_start(self.b, True, False, 0)
        self.a=gtk.Button("    Exit   ")
        self.a.connect("clicked", self.destroy)
        self.hobox.pack_start(self.a, True, False, 0)
    def main(self):
if __name__ == "__main__":
    hello = HelloWorld()

Here is the screen shot:


To get the output…………?

* Copy and paste the code in text editor.

* save the filename with .py extenstion

* run the file with the following command in terminal python filename.py

* You will get a window like above 🙂

2. Simple Currency Converter:

import pygtk
import gtk
class Conversion:
    def convert(self,widget):
    def delete_event(self, widget, event, data=None):
        print "delete event occurred"
        return False
    def destroy(self, widget, data=None):
        print "Completely quit"
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)
        table=gtk.Table(4, 2, True)
        self.label1=gtk.Label("Enter USD Amount: ")
        table.attach(self.label1, 0, 1, 0, 1)
        table.attach(self.text1, 1,2,0,1)
        self.label2=gtk.Label("Indian Value Is (INR): ")
        table.attach(self.label2, 0,1,1,2)
        table.attach(self.text2, 1,2,1,2)
        self.label3=gtk.Label("Euro Value Is (EUR): ")
        table.attach(self.label3, 0,1,2,3)
        table.attach(self.text3, 1,2,2,3)
    def main(self):
if __name__ == "__main__":
    conversion = Conversion()

Screen shot :

To get the output…………?

As mentioned above do it here also.

* Copy and paste the code in text editor.

* save the filename with .py extenstion

* run the file with the following command in terminal python filename.py

* You will get a window like above 🙂

It will help to beginner to learn pygtk.

Thank You

With Regards,
V. Balakrishnan.

User Input as Password in Python

Hi Guys,

Probably, we can know how to get an input from user using raw_input(); Like in some cases we have to get input from user in authentication process at that time we might be a get user password from user. If user enters the password means it should be invisible to others for that there is an option to get the input depending to password.

We have to import a library called getpass With the help of this library we can get the user input as password. Examples are as follows.

import getpass
pw=getpass.getpass("Enter Your Password Here: ")
if (pw=="password"):
print "You are Welcome..."
print "Sorry! Your are not allowed."

Copy this Code and Paste it in any text editor with file extension of .py; If you execute the code means the terminal waits for user input at “Enter Your Password Here: ” here if you type your password it is invisible to you but it has been storing your password into variable name called pw. After then it has been processed with the string for further Statements. If the condition true means True statements executed other wise false statement will executed.


password inputIn the figure you can see that cursor waits for user input.

bala@bala-desktop:~$ python pass.py
Enter Your Password Here:
You are Welcome.

Hope that these information is useful for you. 🙂

Thank You. 🙂

With Regards,
V. Balakrishnan.

About ChennaiPY meet on 26-02-’11

Hello friends,

Greetings to Everyone.

After long period I had chance to attend the chennaipy monthly meeting. Actually ChennaiPy is a Python group which is conducting in Chennai at every last week of the month. I have decided to attend that meeting so i went to Chennai from Kanchipuram at 1’o clock. I think its too late but i had some works which is very urgent to do. So only i have started on that time. I reached there at 3.45 totally i missed 40 minutes Speech.

When i am entering into the class i saw that Mr. Lakshmi Vyas handling the session with the topic called “Meta Programming”. It is totally new concept for me not only concept that word too. Anyhow i listened it very carefully because it is new concept for me. He explained the class fully in Processional  English, but i cant understand his Hand-writing which was written in that board. After some time he wrote in Capital letters to understand his hand writing.

He explained some programs in live to get clear of that concept. It contains totally 7 programs due to time constraint he can cover upto 5th program. He told that we can cover it on next session but he gave overview of that remaining 2 programs.

From his explanations i got some information
* python Decorators
*Function Returns Function

Also he show a real time project that contains meta programming which was created by himself called “HYDE”.

I Feel that the  Session is very useful and advanced for me…

Thats it.