Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

python - 为什么要使用def main()? [重复](Why use def main()? [duplicate])

Possible Duplicate:

(可能重复:)
What does if __name__== "__main__" do?

(if __name__== "__main__"怎么办?)

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?

(我只是想知道主要有没有押韵?)

  ask by Wizzard translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

如果没有主要的标记,即使脚本作为模块导入,代码也会被执行。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...