这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
当前回答
所有答案都解释了功能性,但我会给出一个例子它的使用,这可能有助于进一步清理这个概念。
假设你有两个 Python 文件, a.py 和 b.py. 现在, a.py 输入 b.py. 我们运行 a.py 文件,其中输入 b.py 代码首先执行。
在 b.py 代码中,有一些代码是专属于 b.py 文件,我们不希望任何其他文件(除了 b.py 文件),已进口了 b.py 文件,运行它。
如果它是主要文件(即,b.py)运行代码,在这种情况下它不是(a.py是主要文件运行),那么只有代码被执行。
其他回答
当您互动运行Python时,本地 __name__ 变量被分配为 __main__ 的值。 同样,当您从命令行中执行一个Python 模块时,而不是将其导入到另一个模块时,其 __name__ 属性被分配为 __main__ 的值,而不是模块的实际名称。
if __name__ == '__main__':
# Do something appropriate here, like calling a
# main() function defined elsewhere in this module.
main()
else:
# Do nothing. This module has been imported by another
# module that wants to make use of the functions,
# classes and other useful bits it has defined.
开发和测试您的代码
def do_important():
"""This function does something very important"""
do_important()
以此为主,以此为主,以此为主。
~$ python important.py
问题
但是,如果你想将模块导入到另一个脚本:
import important
# do_important() # I must remember to uncomment to execute this!
更好的方式
if __name__ == "__main__":
do_important()
但是,在这一点上,有一个皮托尼的改善方式。
如果我们想从模块之外运行这个业务过程,怎么办?
如果我们把我们想要练习的代码,当我们开发并测试这样的功能时,然后立即检查“__main__”之后:
def main():
"""business logic for when running this module as the primary one!"""
setup()
foo = do_important()
bar = do_even_more_important(foo)
for baz in bar:
do_super_important(baz)
teardown()
# Here's our payoff idiom!
if __name__ == '__main__':
main()
我们现在有一个最终功能,我们的模块的结束将运行,如果我们运行模块作为主模块。
它将允许模块及其功能和类被进口到其他脚本,而不运行主功能,并将允许模块(及其功能和类)在从不同的“__main__”模块运行时被召唤,即。
import important
important.main()
Python 主要功能是任何程序的起点. 当程序运行时,Python 翻译器连续运行代码. 主要功能只有当它作为 Python 程序运行时才运行。
def main():
print("I am in the function")
print("I am out of the function")
当你运行脚本显示:
I am out of the function
而不是代码“我在职位”。
这是因为我们没有宣布呼叫函数“if__name__==“主”。
如果你使用它:
def main():
print("I am in the function")
if __name__ == "__main__":
main()
print("I am out of the function")
产量相当于
I am in the function
I am out of the function
在Python中,如果__name__==“__main__”允许您运行Python文件作为可重复使用的模块或单独的程序。
当Python 翻译器阅读源文件时,它将执行所有源文件中的代码;当Python 作为主程序运行“源文件”时,它设置了特殊变量 __name__ 以具有“__main__”值。
当您执行主函数时,它将读取如果声明,检查 __name__ 是否相当于 __main__。
如果 __name__ ==“__main__”意味着如果您正常运行 Python 文件如 python foo.py,则将特殊变量 __name__ 归分为“__main__”。
但是,如果您正在导入文件如“导入 foo”,则将 __name__ 归分为“foo”并且不会运行函数。
系统(Python 翻译器)为源文件(模块)提供的一些变量,您可以随时获得它们的值,所以,让我们专注于 __name__ 变量/属性:
如果 Python 将此源代码文件作为主程序(即您运行的文件),则将此文件的特殊 __name__ 变量设置为“__main__”值。
如果从另一个模块进口,则 __name__ 将设置到该模块的名称。
所以,在你的例子中,部分:
if __name__ == "__main__":
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
这意味着代码区块:
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
希望这能帮出来。