为什么main()函数? [英] why a main() function?

查看:105
本文介绍了为什么main()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我在某个地方读了一个建议来包装代码,其中一个Python

脚本在main()函数中启动,所以有一个


def main():

print" hi"


main()


而不是


打印hi


这样做有什么好处?

解决方案


be*******@aol.com 写道:


我想我在某个地方读了一个建议来包装Python

脚本在main()函数中启动的代码,所以一个已经


def main():

打印" hi"


main()


而不是


print" hi"


这样做有什么好处?



我确定还有其他原因,但对我来说最重要的是

您可以将代码导入解释器并从那里戳它

而不执行脚本。当然,您也可以将

代码重新用作另一个程序中的模块。


-Ben


2006年9月18日12:40:00 -0700,是****** *@aol.com < be ******* @ aol.comwrote:


我想我在某个地方读了一个建议来包装一个Python

脚本在main()函数中启动的代码,所以有一个


def main():

print hi


main()


而不是


print" hi"


这样做有什么好处?



在没有运行它们的情况下导入脚本,

调试或稍后重用它们的部分,以及添加小的

测试模块的脚本以允许它们运行以进行测试。


是******* @ aol.com 写道:


我想我在某个地方读了一个建议来包装一个Python

脚本在main()函数中启动的代码,所以有一个


def main():

print hi


main()


而不是


print" hi"


这样做有什么好处?



将其细化为:


def main():

print" hi"


if __name__ ==" __ main __":

main()


''的优点'' if __name__ ..''语句是你可以导入

脚本而不运行''main''代码,例如来自你的unittest模块。


在一个函数中包含主代码,你可以从你的单元测试中调用这个函数来测试它,并像任何其他函数一样测试它。 br />

另外我通常在main()中添加一个''argv''参数,我使用

而不是sys.argv,所以我可以轻松测试它有不同的论点。


-

Benjamin Niemann

电子邮件:pink at odahoda dot de

WWW: http://pink.odahoda.de/


I think I read a suggestion somewhere to wrap the code where a Python
script starts in a main() function, so one has

def main():
print "hi"

main()

instead of

print "hi"

What are the advantages of doing this?

解决方案


be*******@aol.com wrote:

I think I read a suggestion somewhere to wrap the code where a Python
script starts in a main() function, so one has

def main():
print "hi"

main()

instead of

print "hi"

What are the advantages of doing this?


I''m sure there are other reasons, but for me the most important is that
you can import your code into the interpreter and poke at it from there
without executing the script. Also, of course, you can re-use your
code as a module in another program.

-Ben


On 18 Sep 2006 12:40:00 -0700, be*******@aol.com <be*******@aol.comwrote:

I think I read a suggestion somewhere to wrap the code where a Python
script starts in a main() function, so one has

def main():
print "hi"

main()

instead of

print "hi"

What are the advantages of doing this?

It is useful both importating scripts without running them, for
debugging or reusing parts of them later, and also for adding small
test scripts to modules to allow them to be run for testing purposes.


be*******@aol.com wrote:

I think I read a suggestion somewhere to wrap the code where a Python
script starts in a main() function, so one has

def main():
print "hi"

main()

instead of

print "hi"

What are the advantages of doing this?

Refine this to:

def main():
print "hi"

if __name__ == "__main__":
main()

The advantage of the ''if __name__ ..'' statement is that you can import the
script without running the ''main'' code, e.g. from your unittest module.

Wrapping the main code in a function allows you to call this function from
your unittests and test it like any other function.

Additionally I do usually add an ''argv'' argument to main() which I use
instead of sys.argv, so I can easily test it with different arguments.

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/


这篇关于为什么main()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆