这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
当前回答
如果 __name__ ==“__main__”是当脚本从(说)命令线运行时运行的部分,使用像 python myscript.py 这样的命令。
其他回答
如果 __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__' 。
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__”:相当常见。
它检查一个模块是否正在进口。
换句话说,如果区块内部的代码只有当代码直接运行时才会执行。
让我们看看它使用一个简单的代码打印模块的名称:
# test.py
def test():
print('test module name=%s' %(__name__))
if __name__ == '__main__':
print('call test()')
test()
如果我们通过 python test.py 直接运行代码,则模块名称为 __main__:
call test()
test module name=__main__
什么是 __name__?
哪里
>>> print(__name__)
__main__
>>>
此分類上一篇:file.py:
print(__name__)
结果在 __main__
主持人.py:
def somefunction():
print(__name__)
此分類上一篇:file.py:
import somefile
somefile.somefunction()
结果在Somefile
请注意,当在包或模块中使用时, __name__ 取出文件的名称. 实际模块或包路径的路径不被提供,但有自己的 DunderAlias __file__,这允许这样做。
>>> __name__ = 'Horrify' # Change default from __main__
>>> if __name__ == 'Horrify': print(__name__)
...
>>> else: print('Not Horrify')
...
Horrify
>>>
现在回答如果 __name__ ==“__main__”:
如果是包含代码块的流量控制声明,则将执行,如果所提供的值是真实的,我们已经看到 __name__ 可以采取 __main__ 或它已从中进口的文件名。
这告诉我们,如果运行文件是主要文件(或您正在直接从翻译器运行),那么该条件必须执行。
模块
变量
也可以用 __name__ 做其他,不太常见但有用的事情,我会在这里展示一些:
if __name__ != '__main__':
# Do some useful things
运行一个条件,如果文件是主一个和另一个,如果它不是
if __name__ == '__main__':
# Execute something
else:
# Do some useful things
当您输入某种具有此条件的代码时,它将返回虚假(进口代码内),但将返回正确的代码将运行。