这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
当前回答
在 Python 中,每个模块都有一个称为 __name__ 的属性. __name__ 的值是 __main__ 当模块直接运行时,如 python my_module.py. 否则(如您说 import my_module) __name__ 的值是模块的名称。
一个小例子要简要解释。
脚本测试.py
apple = 42
def hello_world():
print("I am inside hello_world")
if __name__ == "__main__":
print("Value of __name__ is: ", __name__)
print("Going to call hello_world")
hello_world()
我们可以直接这样做。
python test.py
出口
Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world
现在假设我们从另一个脚本中称之为上面的脚本:
编辑 external_calling.py
import test
print(test.apple)
test.hello_world()
print(test.__name__)
当你执行这一点时,
python external_calling.py
出口
42
I am inside hello_world
test
因此,上述是自我解释的,当你从另一个脚本打电话测试时,如果在 test.py 中的 loop __name__ 不会执行。
其他回答
创建一个文件, a.py:
print(__name__) # It will print out __main__
__name__ 总是相当于 __main__ 每当此文件运行时,直接显示此文件是主要文件。
在同一目录中创建另一个文件,b.py:
import a # Prints a
它将打印一个,即所进口的文件的名称。
因此,要显示相同文件的两个不同的行为,这是一个常用的技巧:
# Code to be run when imported into another python file
if __name__ == '__main__':
# Code to be run only when run directly
如果我们有两本书,
#script1.py
print "Script 1's name: {}".format(__name__)
和
#script2.py
import script1
print "Script 2's name: {}".format(__name__)
此分類上一篇: Executing script1
Script 1's name: __main__
执行脚本2的结果是:
Script1's name is script1
Script 2's name: __main__
正如你可以看到的那样, __name__ 告诉我们哪个代码是“主要”模块,这是很好的,因为你只能写代码,并且不需要担心结构问题,如在 C/C++ 中,如果一个文件不执行一个“主要”函数,那么它不能作为一个可执行的编写,如果是这样,它不能用作图书馆。
箭头是进口链接. 对于三个模块,每个试图包含以前的模块代码,有六个文件(九个,计算实施文件)和五个链接. 这使得很难将其他代码纳入一个C项目,除非它是专门编写为图书馆。
此分類上一篇
它是特殊的,当一个Python文件从命令线被召唤时,通常用来召唤一个“主()”函数或执行其他适当的启动代码,如命令线论点处理,例如。
它可以用多种方式写出来,另一个是:
def some_function_for_instance_main():
dosomething()
__name__ == '__main__' and some_function_for_instance_main()
我不是说你应该在生产代码中使用这一点,但它用来解释是否有“魔法”的东西。
它只是在Python文件中引用一个主要功能的条约。
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__':下面的代码将仅执行,如果模块被称为脚本。
作为一个例子,请考虑以下模块 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__' 。