Put Simplysimply, __name__ is a variable defined for each script, that defines whether the script is being run as the main module or it is being run as an imported module.
So if we have two scripts;
#script1.py
print "Script 1's name: {}".format(__name__)
and ;
#script2.py
import script1
print "Script 2's name: {}".format(__name__)
The output from executing script1 is;is
Script 1's name: __main__
andAnd the output from executing script2 is;is:
Script1's name is script1
Script 2's name: __main__
As you can see;see, __name__ tells us which code is the 'main' module.
This is great, because you can just write code and not have to worry about structural issues like in C/C++, where, if a file does not implement a 'main' function then it cannot be compiled as an executable and if it does, it cannot then be used as a library.
Say you write a pythonPython script that does something great and you implement a boatload of functions that are useful for other purposes, if. If I want to use them I can just import your script and use them without executing your program (given that your code only executes within the if __name__ == "__main__": context). Whereas in C/C++ you would have to portion out those pieces into a seperateseparate module that then includes the file. Picture the situation below;
The arrows are import links. For three modules each trying to include the previous modules code there are six files (nine, counting the implementation files) and five links , this. This makes it difficult to include other code into a cC project unless it is compiled specifically as a library. Now picture it for python;Python:
You write a module, Ifand if someone wants to utilizeuse your code they just import it and the __name__ variable can help to seperateseparate the executable portion of the program from the library part.

