这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
当前回答
系统(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))
希望这能帮出来。
其他回答
当您执行模块(源文件)时,条件是否检查模块是否直接被召唤或从另一个源文件被召唤。
如果直接呼吁执行,则模块名称将设置为“主”,然后在如果区块内的代码将执行。
除了已经提供的信息,如果 __name__ ==“__main__”:技术也是一个很好的方式来确保您的最小和最单一的脚本仍然运行,如果你偶然用Python而不是Python(或Python -m最单一)。
def test_assert():
assert 1 + 2 == 3
if __name__ == "__main__":
import pytest
pytest.main([__file__])
import unittest
class Tests(unittest.TestCase):
def test_assert(self):
self.assertTrue(1 + 2 == 3)
if __name__ == "__main__":
unittest.main()
然后,你的脚本与 Python 呼叫相同,就像 Python -m 最单一的呼叫一样。
现在,如果你也想确保所有的麻烦被转移到问答,如果用Python打电话? 或者,如果你也想添加额外的麻烦? 这里有一个例子如何做到这一点:
def test_assert():
assert 1 + 2 == 3
if __name__ == "__main__":
from pytest import main
from sys import argv
main([*argv, "-s"])
现在,您的 python -v --html=report.html 将具有与 pytest -v --html=report.html 等相同的效果,这是一个很好的方式来确保脚本仍然按照意图运行,即使不与预期的 pytest 或 python -m 最单一的通话运行。
# XXX FIXME: useless (see below)
if __name__ == "__main__":
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
如果你想能够进口纤维,第一个版本是无用的,也是,因为有用的代码在一个部分,它不会运行,当你进口这个文件(在这种情况下 __name__ 不会是“__main__”). 正确的设计在这种情况下将是恢复代码,以便有用的部分在一个功能,你可以运行,当你想要之后你已经进口它。
def main():
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
if __name__ == "__main__":
main()
事实上,更好的设计仍然是将可重复使用的部分(实际计算)与用户可见的输入/输出隔离:
def fibn(n: int) -> int:
a, b = 0, 1
while b < n:
a, b = b, a+b
return b
def main() -> None:
n = int(input('Write a number: '))
print('Fibonacci number %i: %i' % (n, fibn(n)))
if __name__ == "__main__":
main()
现在,您可以从 Fib 进口 Fibn 并从执行此进口的代码呼叫 Fibn() 函数。
def main():
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
if __name__ == "__main__":
main()
(不同于类似于C的语言,主要名称对Python没有具体的含义;但它是常见的公约使用它作为将运行的东西的名称。
开发和测试您的代码
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()
如果 __name__ == '__main__':下面的代码将仅执行,如果模块被称为脚本。
作为一个例子,请考虑以下模块 my_test_module.py:
# my_test_module.py
print('This is going to be printed out, no matter what')
if __name__ == '__main__':
print('This is going to be printed out, only if user invokes the module as a script')
第一個選項:將 my_test_module.py 輸入到另一個模組
# main.py
import my_test_module
if __name__ == '__main__':
print('Hello from main.py')
现在,如果你引用 main.py:
python main.py
>> 'This is going to be printed out, no matter what'
>> 'Hello from main.py'
请注意,仅在 my_test_module 中执行顶级印刷() 声明。
第二种可能性:将 my_test_module.py 作为脚本提交
现在,如果你将 my_test_module.py 作为 Python 脚本运行,那么打印() 陈述将运行:
python my_test_module.py
>>> 'This is going to be printed out, no matter what'
>>> 'This is going to be printed out, only if user invokes the module as a script'
对于更全面的解释,您可以阅读在 Python 中做什么 __name__ == '__main__' 。