时间:2021-07-01 10:21:17 帮助过:45人阅读
例子:
# file one.pydef func(): print("func() in one.py") print("top-level in one.py")if __name__ == "__main__": print("one.py is being run directly")else: print("one.py is being imported into another module")
# file two.pyimport one # start executing one.pyprint("top-level in two.py") one.func()if __name__ == "__main__": print("two.py is being run directly")else: print("two.py is being imported into another module")
当运行python one.py,输出:
top-level in one.py one.py is being run directly
当运行python two.py,输出:
top-level in one.py one.py is being imported into another module top-level in one.pyfunc() in one.py two.py is being run directly
以上就是if __name__ == __main__:有什么作用的详细内容,更多请关注Gxl网其它相关文章!