Last updated 5 years ago
Was this helpful?
在Python程序中,经常可以看到如下程序结构
或者
为什么不是直接定义自己的函数,类似
这是因为
如果不使用main函数,代码就会在导入的时候作为一个模块直接运行
main
其他语言(如C和Java)都有一个main()函数在程序执行时调用,使用 if 结构,则更为熟悉
main()
if
定义了较为清晰和易读的代码
def main(): # my code here if __name__ == "__main__": main()
import sys def main(argv): # My code here pass if __name__ == "__main__": main(sys.argv)
def my_function() # my code here def my_function_two() # my code here # some code # call function # print(something)