_name_ _main_ in Python
The main() function is required in programmes developed in the C family of languages (C, C++, Java, C#, etc.) to designate where the execution should begin.
However, because Python is an interpreter-based language that can also be used in an interactive shell, there is no such thing as a main() method in it. Multiple statements can be found in the .py extension Python programme file. The first statement of the Python programme file is where the execution begins.
Python interpreter analyses source file and defines some special variables/global variables before running code. The Python interpreter set the special __name__ variable to have the value "__main__" if that module (the source file) is being executed as the main application. __name__ will be set to the name of the module if the same file is being fetched from another module. The __name__ global variable accepts the module name as a value.
A file containing definitions and statements in Python is known as a module. The module name with the .py extension becomes the file name.
Executed Always It is executed when it's invoked directly
This whole block of code indented at level 0 [Block 1] is run. Although specified functions and classes are well defined, none of their code actually executes.
As we followed the instructions here. The __main__ variable will be used directly by Python. As a result, the code in this if block [Block 2] will only be executed if that module serves as your program's entry point.
So, by checking the __name__ variable, you may determine if your script is getting executed manually or whether it is being imported by another programme.
If script is imported by another module at that moment, the module name will be __name__.
Why is it required?
It is usually required when we want to execute a function directly. For instance, we are creating a script that will be utilised as a module:
I'm inside this function
Advantages of using _name_ _main_ :
Why was this designed this way?
Naturally, we might ponder the rationale behind the design. Well, there are instances when we wish to create a .py file that can be used as both a module by other programmes and/or modules and the main application itself.
This behaviour helps us test and improve our code more quickly. Since it enables us to run unit tests directly in script mode, it also aids in debugging.
Additionally, the fact that setting up a single variable is all it takes to launch a module in Python directly is elegant.
The __name__ variable has a particularly excellent use case, whether you need a file that can be imported by the other modules or launched as the primary programme. When the modules are imported, we can allow or disallow running specific sections of code by using an if __name__ == "__main__" block.
The __name__ variable is set to the name of the imported module or __main__ if the module is being launched whenever the Python interpreter reads any file. All top-level code is executed when reading the file, but functions and classes are not (as they will only be imported).
By employing the top-level scope __main__ and the special variable __name__, the reusability is increased. Both independently and when imported as a module, the Python script file can be run from the command line or terminal.
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week