+ 2
When it is used and why: if __name__ == "__main__":
3 Respostas
+ 10
I use that code idiom for unit tests, so that the test libraries are imported and the tests are run only when the code is run directly. If the file is imported as a module into other code, __name__ == "__main__" is false, and the tests are skipped.
+ 7
This stackoverflow post should help you out
https://stackoverflow.com/questions/419163/what-does-if-name-main-do
+ 7
__name__ returns the scope of the function.
If the file is a executed, __name__="__main__"
However, if it is imported as a module, it won't.
In other words,
commands under the 'if __name__=="__main__"'
will be executed if it is run as the main acript but will not run if it is imported as a module.