sample.py
import ~~
def hoge
#~~
if __name__ = "__main__":
#~~
I see this kind of code, but what is `` `name```?
There are some special variables that the python interpreter defines before executing code, one of which is the `__name__``` variable. And if the module you are reading, the source code, is the main program, python will set the __name__``` variable to the value `` " main ". If the file is imported from another module, `` `__name__ will contain the module name.
That is, on the console
python sample.py
If you do, it means that the `__name__``` variable is set to `main``` in sample.py that is running as the main program.
In sample.py above, we set these special first defined variables, then execute the import declarations, then `` `defEvaluate the block and create a variable that references a function object called hogehoe. Then go to the if``` statement and perform the processing in it.
By this procedure, the processing in if can be executed with the defined function or the imported module.
The meaning of having this is that I don't want it to be executed when it is imported from another module, but when I run it as the main program, I do it and process it. It's the same as how to think of the main function of C ++ or java.
I see.
Recommended Posts