__name__=='__main__' in Python


__name__ is a builtin special variable. Every python script or module has this variable, and the interpreter sets the the value of __name__ variable as "__main__". If the python module is imported in some other script file, the __name__ will be set to the module name for imported module.

Example

file name : myscript1.py

print("value of __name__ of myscript1 is ",__name__)                    
                
OUTPUT

value of __name__ of myscript1 is  __main__
                

As we ca see in the above example, when we execute the myscript1.py script file, the value of __name__ is __main__ (which is string).

Now, let's create another script file and import the myscript1.py module and see the output.


file name : myscript2.py

import myscript1
print("value of __name__ of myscript2 is ",__name__)                    
                
OUTPUT

value of __name__ of myscript1 is  myscript1
value of __name__ of myscript2 is  __main__
                

Since myscript2.py file is executing now, the value of __name__ will __main__ for the current script file and for imported module it will be the module name, as we can see in the output for myscript1.py, the value of __name__ is myscript1.


if __name__ == "__main__"

One thing you have to remember that, whatever the statement we will write at level 0 indentation is to be executed automatically , either you are running the current python file or the file is being imported.


file name : myscript1.py

def myfunc():
    print("function from myscrip1")

print("Hello")
print("Hi")
myfunc()
                    
                
OUTPUT

Hello
Hi
function from myscrip1
                

From the above code, we can see that two print statements and one function calling are at level 0 indentation, as we execute the script file all the statements executed.

Now, let's import the file in another file and check the output


file name : myscript2.py

import myscript1
                    
                
OUTPUT

Hello
Hi
function from myscrip1
                

See the above code, just importing the module myscript1, and get the same output. Actually, as we import any module, the level 0 indented statements will execute.

Use of if __name=="__main__"

As we have seen in the above examples, the level 0 indented statements executed automatically. So, if we we want to control that the statements should run when the current python script is running and in some other module when we call the particular function then we use if __name__=="__main__" conditional statement.

file name : myscript1.py

def myfunc():
    print("function from myscrip1")

if __name__ == '__main__':
    print("Hello")
    print("Hi")
    myfunc()
                    
                
OUTPUT

Hello
Hi
function from myscrip1
                

Whe we execute myscript1 then we get the all outputs, because in this case we will get the value of __name__ as __main__, and the if conndition will become true.

Now, if we import the module then the the code inside the if will not execute because in that case the value of __name__ will be myscript1, and the if conndition will become false.

Example

file name : myscript2.py

import myscript1
                    
                
OUTPUT

                

As we can see, there is no output.

Now, we will call the fuction from myscript1 to myscript2 and check whether the fuction is being called or not.


file name : myscript2.py

import myscript1
myscrip1.myfunc()
                    
                
OUTPUT
function from myscrip1
                

Most of the programmig languages like C, C++, Java has the main function as entry point for the execution. But in python, statemets start execution from very first statemet from level 0 identation. So __main__ can be used as main in python.

Next chapter is Anonymous Function





Video Lecture



 




Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017, prowessapps.in, All rights reserved