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.

 

Advertisements

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

  1. Hi,

    I usually use this __name__ == ‘__main__’: block statement to call the script functions or object creations or getting user inputs and call the corresponding function accordingly.

    eg1:

    #foo1.py

    def add(a, b): return a+b

    if __name__ == ‘__main__’:

    a = input(“Enter no a : “) b = input(“Enter no b : “) print “Addition = %d” % add(a, b)

    eg2:

    #foo2.py

    from foo1 import add print add(5,4)

    In this eg2, the foo1 module’s if __name__ == ‘__main__’: statements (i.e. main block statements) will not be executed, because we just imported our needed function ‘add’ alone using from statement.

    • Hope you can understand the python indentation.

       
      if __name__ == ‘__main__’:  
             a = input(“Enter no a : “)
             b = input(“Enter no b : “)  
             print “Addition = %d” % add(a, b) 
      
    • Okey. So,

      Exact place of using that statement is:

      If i want to avoid/perform few statements, if a program has been executed as an Individual program/imported in a program.
      In those scenarios I can use if(__name__) statement.

      Did I catch your point? 😉

  2. Nice post. I was checking continuously this blog and I am impressed!
    Extremely helpful information specifically the last
    part 🙂 I care for such info much. I was seeking this certain information for a long time.

    Thank you and best of luck.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s