Python main function
This question already has an answer here:
you can think this as the main() in C or the BEGIN { } block in perl.
when you run the code using python file1.py.
__name__
in file1.py is equal to '__main__'
, but in other files imported by file1.py, the variable is something else.
If you execute your script directly, without importing it, __name__
will be equal to __main__
. But if you import this file, __name__
will be equal to the name of the module importing it. This condition makes sure you execute your code from this file.
上一篇: Python中的执行流程
下一篇: Python的主要功能