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

Hi,

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.
Example:

#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

Output:

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……

Regards,
Balakrishnan V.