Analysis of if __name__ == '__main__': in python

When you open a .py file, you often see if __name__ == '__main__': at the bottom of the code, now let's introduce its role.
Modules are objects, and all modules have a built-in attribute __name__. The value of a module's __name__ depends on how you apply the module. If importing a module, the value of module __name__ is usually the module filename, without a path or file extension. But you can also run the module directly like a standard program, in which case the value of __name__ will be a special default "__main__".
///////////////////////////////////////////////////////////////////////////////////////////////////
Run the .py file directly in cmd, the value of __name__ is '__main__';
And after importing a .py file, the value of __name__ is not '__main__';
So use if __name__ == '__main__' to determine whether the .py file is running directly

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324516252&siteId=291194637