这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
当前回答
如果 Python 翻译器运行一个特定的模块,那么 __name__ 全球变量将有“__main__”的值:
def a():
print("a")
def b():
print("b")
if __name__ == "__main__":
print ("you can see me")
a()
else:
print ("You can't see me")
b()
当你运行这个脚本时,它打印:
you can see me
a
如果您输入此文件,请说 A 到 B 文件,然后执行 B 文件,然后如果 __name__ == “__main__” 在 A 文件中变成错误,那么它打印:
You can't see me
b
其他回答
例如,这是 test1.py 没有如果 __name__ ==“__main__”::
# "test1.py"
def hello()
print("Hello")
hello()
而且 test2.py 只輸入 test1.py:
# "test2.py"
import test1 # Here
然后,当运行 test2.py 时,一个 Hello 被打印,因为 test1.py 中的不需要的 hello() 代码也被运行:
python test2.py
Hello
当然,您可以在 test2.py 中拨打 test1.hello():
# "test2.py"
import test1
test1.hello() # Here
然后,当运行测试2时,印刷了两个Hello:
python test2.py
Hello
Hello
现在,添加如果 __name__ ==“__main__”:测试1.py 并将 hello() 放在下面:
# "test1.py"
def hello()
print("Hello")
if __name__ == "__main__":
hello()
此分類上一篇: Test2.py:
# "test2.py"
import test1
test1.hello()
然后,在运行 test2.py 时,只有一个 Hello 被打印,因为如果 __name__ ==“__main__”:在 test2.py 输入 test1.py 时阻止运行不需要的 hello() 代码:
python test2.py
Hello
此外, test1.py 是否有 __name__ == “__main__”: 或者不:
# "test1.py"
def hello()
print("Hello")
if __name__ == "__main__":
hello()
# "test1.py"
def hello()
print("Hello")
hello()
One Hello 在运行 test1.py 时正确打印:
python test1.py
Hello
您可以将文件作为一个脚本以及一个可进口的模块使用。
fibo.py(一个名为fibo的模块)
# Other modules can IMPORT this MODULE to use the function fib
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print(b, end=' ')
a, b = b, a+b
print()
# This allows the file to be used as a SCRIPT
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
参考: https://docs.python.org/3.5/tutorial/modules.html
什么是 __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
我们看到是否 __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__
当我们模块(M.py)中有某些声明时,我们希望当它作为主要(不进口)运行时,我们可以将这些声明(测试文件夹、印刷声明)置于此下,如果它被阻止。
默认情况下(当模块作为主运行时,不进口) __name__ 变量设置为“__main__”,当它被进口时, __name__ 变量将获得不同的值,最有可能是模块的名称(“M”)。
简而言之,使用这个“如果 __name__ ==“主””块,以防止(一定)代码在模块进口时运行。