If __name__ == '__main__' in Python 3

What does If __name__ == '__main__' mean in Python ?

As we know that in Python we don't have a definite main functions, we don't require it normally in a program as most programs run flawlessly. But when we are working on more than one python program files and we want to import a certain function of Python_Project 1 in Python_project 2, then we have to declare the main function as   If __name__ == '__main__'  because if we don't do it the whole main program will run along the function we wanted to run from Python_Project 1 in Python_project 2. 

Like in C, C++ we use a main function similarly it is a type of main function which is used when required. 

We require two python files to see it's proper execution.

We get the output as 10 in Project 2.


See Project 1 and Project 2 we are using add function from Project 1 in Project 2 through normal importing, but we have declaired the main here. If we haven't have declaired the main function the result which we would get is follows.

And it's result is 
Here we can clearly see the main function which we haven't declared run when we run the add function in Project 2. So to avoid this problem the main function is used.

In most compilers type "main" and press "TAB" and the whole syntax of  If __name__ == '__main__' which will be auto-written.

Comments

Popular posts from this blog

Bubble Sort ( C & Python 3)

Comparison Logical and Bitwise Operator ( Java Part - 4 )

Something about me