Python main function

This question already has an answer here:

  • What does if __name__ == “__main__”: do? 23 answers

  • 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.

    链接地址: http://www.djcxy.com/p/9318.html

    上一篇: Python中的执行流程

    下一篇: Python的主要功能