Skip to main content
Active reading [<http://en.wikipedia.org/wiki/Python_%28programming_language%29> <http://en.wiktionary.org/wiki/seperate> <http://en.wikipedia.org/wiki/User:Tony1/How_to_improve_your_writing#Misplaced_formality>]
Source Link
Peter Mortensen
  • 31.1k
  • 22
  • 111
  • 134

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;

complicated importing in CComplicated importing in C

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:

elegant importing in PythonElegant importing in 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.

Put Simply __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;

Script 1's name: __main__

and the output from executing script2 is;

Script1's name is script1 
Script 2's name: __main__

As you can 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 python script that does something great and you implement a boatload of functions that are useful for other purposes, 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 seperate module that then includes the file. Picture the situation below;

complicated importing in C

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 makes it difficult to include other code into a c project unless it is compiled specifically as a library. Now picture it for python;

elegant importing in Python

You write a module, If someone wants to utilize your code they just import it and the __name__ variable can help to seperate the executable portion of the program from the library part.

Put simply, __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

Script 1's name: __main__

And the output from executing script2 is:

Script1's name is script1
Script 2's name: __main__

As you can 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 Python script that does something great and you implement a boatload of functions that are useful for other purposes. 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 separate module that then includes the file. Picture the situation below;

Complicated importing in C

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 makes it difficult to include other code into a C project unless it is compiled specifically as a library. Now picture it for Python:

Elegant importing in Python

You write a module, and if someone wants to use your code they just import it and the __name__ variable can help to separate the executable portion of the program from the library part.

added 3 characters in body
Source Link
redbandit
  • 2.3k
  • 18
  • 12

Put Simply __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;

Script 1's name: __main__

and the output from executing script2 is;

Script1's name is script1 
Script 2's name: __main__

As you can 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 python script that does something great and you implement a boatload of functions that are useful for other purposes, 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 seperate module that then includes the file. Picture the situation below;

complicated importing in C

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 makes it difficult to include other code into a c project unless it is compiled specifically as a library. Now picture it for python;

elegant importing in Python

You write a module, If someone wants to utilize your code they just import it and the __name__ variable can help to seperate the executable portion of the program from the library part.

Put Simply __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;

Script 1's name: __main__

and the output from executing script2 is;

Script1's name is script1 
Script 2's name: __main__

As you can 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 python script that does something great and you implement a boatload of functions that are useful for other purposes, 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 seperate module that then includes the file. Picture the situation below;

complicated importing in C

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 makes it difficult to include other code into a c project unless it is compiled specifically as a library. Now picture it for python;

elegant importing in Python

You write a module, If someone wants to utilize your code they just import it and the __name__ variable can help to seperate the executable portion of the program from the library part.

Put Simply __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;

Script 1's name: __main__

and the output from executing script2 is;

Script1's name is script1 
Script 2's name: __main__

As you can 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 python script that does something great and you implement a boatload of functions that are useful for other purposes, 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 seperate module that then includes the file. Picture the situation below;

complicated importing in C

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 makes it difficult to include other code into a c project unless it is compiled specifically as a library. Now picture it for python;

elegant importing in Python

You write a module, If someone wants to utilize your code they just import it and the __name__ variable can help to seperate the executable portion of the program from the library part.

Source Link
redbandit
  • 2.3k
  • 18
  • 12

Put Simply __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;

Script 1's name: __main__

and the output from executing script2 is;

Script1's name is script1 
Script 2's name: __main__

As you can 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 python script that does something great and you implement a boatload of functions that are useful for other purposes, 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 seperate module that then includes the file. Picture the situation below;

complicated importing in C

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 makes it difficult to include other code into a c project unless it is compiled specifically as a library. Now picture it for python;

elegant importing in Python

You write a module, If someone wants to utilize your code they just import it and the __name__ variable can help to seperate the executable portion of the program from the library part.