为什么使用def main()? [英] Why use def main()?
本文介绍了为什么使用def main()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
如果__name __ ==__main __
做什么?
我看过一些使用
的代码示例和教程:def main():
pre>
#我的代码在这里
如果__name__ ==__main__:
main()
但是为什么?有没有任何理由不定义你的函数在文件的顶部,然后只是写下代码在它下面?
def my_function()
$ p $
#我的代码在这里
def my_function_two b $ b#我的代码在这里
#一些代码
#调用函数
#print(something)
解决方案我想知道是否有任何韵律。没有主哨兵,即使脚本作为模块导入,代码也会被执行。
Possible Duplicate:
What doesif __name__== "__main__"
do?I've seen some code samples and tutorials that use
def main(): # my code here if __name__ == "__main__": main()
But why? Is there any reason not do define your functions at the top of the file, then just write code under it? ie
def my_function() # my code here def my_function_two() # my code here # some code # call function # print(something)
I just wonder if there is any rhyme to the main?
解决方案Without the main sentinel, the code would be executed even if the script was imported as a module.
这篇关于为什么使用def main()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文