Why do some python scripts declare main() in this manner?

This question already has an answer here: What does if __name__ == “__main__”: do? 23 answers This line allows you to make some functionality run by default only when you run the script as the main script (eg python my_script.py ). This is useful when the script may be used either as a main program or to be imported in another python module, or python shell. In the latter case you would a

为什么一些python脚本以这种方式声明main()?

这个问题在这里已经有了答案: 如果__name__ ==“__main__”:做什么? 23个答案 只有当您将脚本作为主脚本运行时(例如python my_script.py ),此行才允许默认运行某些功能。 当脚本既可以作为主程序使用,也可以在另一个python模块或python shell中导入时,这很有用。 在后一种情况下,你几乎肯定不希望main(或其他模块功能)在导入时运行,这是默认情况下当解释器加载脚本时发生的情况。 如果你永远不会在其他代码

Confused with two keywords:

This question already has an answer here: What does if __name__ == “__main__”: do? 23 answers __name__ will give you the name of the current module and module which is used when you say python prog.py __name__ in prog.py would now be __main__ . This has been explained in python docs here

与两个关键字混淆:

这个问题在这里已经有了答案: 如果__name__ ==“__main__”:做什么? 23个答案 __name__会给你当前模块和模块的名称,当你说 python prog.py __name__中的prog.py现在是__main__ 。 这已经在python docs中解释了

Defining main() in python

Possible Duplicate: What does <if name ==“ main ”:> do? Often, I see the following code in python programs if __name__ == '__main__': main() I'm following the Python Class on Google code, and it says that it's standard boilerplate code. Do I really need to write such code in all my scripts? What functionality would this add to my programs? No, you don't have to, but

在python中定义main()

可能重复: <if name ==“ main ”:>是做什么的? 通常,我在python程序中看到以下代码 if __name__ == '__main__': main() 我正在关注Google代码上的Python类,并且说它是标准的样板代码。 我真的需要在我的所有脚本中编写这样的代码吗? 这会添加到我的程序中的功能是什么? 不,你不必这样做,但对于单元测试这样的东西是非常宝贵的。 您可以在每个python文件中创建一个main ,以便如果直接运行它, __nam

How to run python functions in unix?

Possible Duplicate: What does <if __name__==“__main__”:> do? Whenever I tried to run my python script in unix nothing would happen. I'd type something along the lines $ python script.py and all that would be returned is $ Now I know it isn't a problem in my code because that runs fine in idle, so I figured I needed to add something else to my code to be able to run it fro

如何在unix中运行python函数?

可能重复: <if __name __ ==“__ main __”:>做什么? 每当我试图在unix中运行我的python脚本时,什么都不会发生。 我会沿着线输入一些东西 $ python script.py 所有将返回的是 $ 现在我知道它在我的代码中没有问题,因为它在闲置状态下运行良好,所以我想我需要在代码中添加其他内容以便能够从命令行运行它。 在一个关于python的Google教程中,我介绍了一些样板代码,这些代码粘贴到函数的末尾 def main():

Flow of execution in Python

This question already has an answer here: What does if __name__ == “__main__”: do? 23 answers The Python interpreter knows nothing of a main() function - the flow is just line by line. The block that goes: if __name__ =="__main__": main() is a explicit call to a function if the magic variable __name__ contains the string "__main__" . That thing, the content of __name__ is the

Python中的执行流程

这个问题在这里已经有了答案: 如果__name__ ==“__main__”:做什么? 23个答案 Python解释器对main()函数一无所知 - 流只是一行一行。 该块: if __name__ =="__main__": main() 是一个明确的函数调用,如果魔术变量__name__包含字符串"__main__" 。 那东西, __name__的内容是Python运行时运行模块的一件特别的事情:如果当前模块是被调用的主程序,它包含字符串__main__ ,否则它的内容就是模块名称。

Python main function

This question already has an answer here: What does if __name__ == “__main__”: do? 23 answers you can think this as the main() in C or the BEGIN { } block in perl. when you run the code using python file1.py. __name__ in file1.py is equal to '__main__' , but in other files imported by file1.py, the variable is something else. If you execute your script directly, without importi

Python的主要功能

这个问题在这里已经有了答案: 如果__name__ ==“__main__”:做什么? 23个答案 你可以认为这是C中的main()或perl中的BEGIN {}块。 当你使用python file1.py运行代码时。 __name__中的__name__等于'__main__' ,但在由file1.py导入的其他文件中,变量是别的。 如果直接执行脚本,而不导入它, __name__将等于__main__ 。 但是,如果您导入此文件, __name__将等于导入它的模块的名称。 这个条件确保你从这

I don't understand Python's main block. What is that thing?

Possible Duplicate: What does <if __name__==“__main__”:> do? So I start up pyscripter and I get a file with this in it: def main(): pass if __name__ == '__main__': main() What is that? Why does my program work without it as well? Whats the purpose for this anyway? Where would my code go? Lets say aa function that prints hello world. Where would that go? where would I ca

我不明白Python的主要区块。 那个东西是什么?

可能重复: <if __name __ ==“__ main __”:>做什么? 所以我启动了pyscripter,并在其中获得了一个文件: def main(): pass if __name__ == '__main__': main() 那是什么? 为什么我的程序在没有它的情况下工作? 无论如何,这是什么目的? 我的代码在哪里? 让我们说一个打印hello世界的函数。 那去哪里? 我会在哪里叫它? 目的基本上是,只要脚本直接运行,您就可以定义一个中央入口点。 因为

Why does it do this ? if

Possible Duplicate: What does <if name ==“ main ”:> do? Consider this code: if __name__ == '__main__': import pdb pdb.run("interact()n") What does the following line mean? if(__name__=='__main__') I fainted. This will be true if this module is being run as a standalone program. That way, something can act either as a module imported by another program, or a standalone pro

它为什么这样做? 如果

可能重复: <if name ==“ main ”:>是做什么的? 考虑这个代码: if __name__ == '__main__': import pdb pdb.run("interact()n") 以下行意味着什么? if(__name__=='__main__') 我昏了过去。 如果此模块作为独立程序运行,这将是真实的。 这样,某些东西既可以作为另一个程序导入的模块,也可以作为独立程序运行,但只有在if语句中执行代码才能作为程序执行。 __name__是一个在执行python程序中自动

What does `if name == "

Possible Duplicate: What does <if name ==“ main ”:> do? I have wrote scripts in Python for quite a while now and I study more of Python as I need it. When reading other people's code I meet if name == "__main__": construct quite often. What is it good for? This allows you to use the same file both as a library (by importing it) or as the starting point for an applicat

“if name ==”是什么

可能重复: <if name ==“ main ”:>是做什么的? 我已经用Python编写了脚本了很长一段时间,并且我在需要时学习了更多的Python。 当阅读其他人的代码时,我遇到了if name == "__main__":经常构造。 到底有什么好处呢? 这使您可以将相同的文件作为库(通过导入它)或作为应用程序的起点使用。 例如,请考虑以下文件: # hello.py def hello(to=__name__): return "hello, %s" % to if __name__

Understanding the main method of python

This question already has an answer here: What does if __name__ == “__main__”: do? 23 answers The Python approach to "main" is almost unique to the language(*). The semantics are a bit subtle. The __name__ identifier is bound to the name of any module as it's being imported. However, when a file is being executed then __name__ is set to "__main__" (the literal str

了解python的主要方法

这个问题在这里已经有了答案: 如果__name__ ==“__main__”:做什么? 23个答案 对于“主要”的Python方法几乎是独特的语言(*)。 语义有点微妙。 __name__标识符在导入时绑定到任何模块的名称。 然而,正在执行的文件,然后当__name__被设置为"__main__" (文字串: __main__ )。 这几乎总是用于将应该执行的代码部分与定义功能的代码部分分开。 所以Python代码通常包含如下一行: #!/usr/bin/env python