Reference site: [Introduction to Python] What is the important "if name =='main':" when dealing with modules?
When I'm studying Python and looking at sample programs, I often see the sentence "if name =='main':". Main is indispensable for writing programs in other programming languages such as C and Java, but in Python, main is not usually used when coding.
However, this sentence plays an important role in working with modules in Python. This time, I will explain what this "if name =='main':" means.
Python code can be saved as a script file and reused by other programs. That file is called a module. "Import" is used when loading the standard library in Python, but by loading your own module using import, you can use the functions and classes defined in that module in other programs as well.
Now that you know what the module means, let's talk about the variable "name". As an example, suppose you have the following program test.py.
test.py
def test():
print('Hello World!')
if __name__ == '__main__':
test()
The result of executing this program is as follows.
Hello World!
In this example, we just defined a function called test that displays "Hello World!". I haven't declared the variable "name" anywhere. Nevertheless, the fact that test () is being executed means that the if statement is returning True. What exactly does that mean?
Let's first look at this variable called "name". In fact, this variable name is created automatically when you load a Python script on a Python printer. In addition, the module name of the script being executed is automatically assigned to it.
I found that a variable called name was created automatically, and the module name of the script being executed was automatically assigned. However, I don't remember making a module called main. Where does this main come from?
In fact, when you run a Python script directly, the script file will be recognized as a module named "main". Therefore, if you execute the script file directly, the value main will be automatically assigned to the name variable.
In other words, the meaning of "if name =='main':" was "execute only if it is executed directly, not execute otherwise". In the previous example, we ran test.py directly, so the if statement returned True and test () was ran.
As a test, I added a process to display the executed module name in test.py.
def test():
print('Hello World!')
if __name__ == '__main__':
test()
print('Module name:{}'.format(__name__)) #Display the name of the executed module
If you do this, you will get the following results:
Hello World! Module name: main
When you execute the script file directly, you can see that the module name is "main".
So what does "except when run directly" mean? That is when I read this script file as a module from another program. I created test2.py which imports test.py as a module.
test2.py
import test #test.Import py
Execution result
Module name: test
If you import and run test.py, you will see that the module name is "test". That is, test.py is running as a test module, so name is assigned "test". Therefore, "if name =='main':" returns False, so test () was not executed.
"If name =='main':" is used for testing while creating a module, or when you do not intend to modularize at the moment but want to avoid unnecessary processing when modularizing. You may not think about modularization or reuse while you are making a program by yourself, but it is an essential idea when you make a large-scale program. Let's remember it.
Recommended Posts