这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
当前回答
假设我在维基百科上写了网页扫描的功能和课程,当然,这可能不是一个好例子。
我想在另一个程序中使用这些功能,而不重写它。
好吧,我进口它们,但在该文件的结尾我把 __name__ == '__main__'
当我们进口一个模块时,它内部的所有代码都从开始到结束,但当它达到条件时,它不运行函数、函数2等,即维基百科 __scrape__。
好吧,在全球范围内,Python __name__ 为当前程序定义为“__main__”。
当我们进口一个模块时,它被定义为我们当前程序的名称空间中的变量,而当前程序 __name__ 是“__main__”:
文件测试.py
def func():
# Do something
pass
def func2():
# Do something
pass
print('The program name is set to ', globals()['__name__'])
if __name__=='__main__':
# In the current program, __name__ is equal to '__main__'
func('https://www.wikipedia.org')
func2('https://www.wikipedia.org')
# Or do more jobs
import test1
print('inside of current program')
print('name is current program', __name__)
print(globals()['test1'])
test1.func('another site')
test1.func2('another site')
出口
inside of test 1
name of program is set to test1
end of module
inside of current
__main__
<module 'test1' from 'C:\\users\\ir\\appdata\\local\\programs\\python\\python38\\lib\\test1.py'>
其他回答
开发和测试您的代码
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__”:相当常见。
它检查一个模块是否正在进口。
换句话说,如果区块内部的代码只有当代码直接运行时才会执行。
让我们看看它使用一个简单的代码打印模块的名称:
# 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__ ==“__main__”:该怎么办?
__name__ 是一个全球变量(在 Python 中,全球实际上意味着在模块层面)存在于所有名称空间。
作为唯一的特殊情况,但是,在任何Python过程你运行,如 mycode.py:
python mycode.py
否则匿名的全球名称空间将“__main__”的值归咎于其 __name__。
因此,包括最后的线条
if __name__ == '__main__':
main()
在您的 mycode.py 脚本的结尾,当它是由 Python 过程运行的主要输入点模块时,
将使您的脚本独特定义的主要功能运行。
使用此构造的另一个好处:您也可以将代码作为一个模块进口到另一个脚本,然后运行主功能,如果和当您的程序决定:
import mycode
# ... any amount of other code
mycode.main()
如果我们有两本书,
#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项目,除非它是专门编写为图书馆。
此分類上一篇