给定以下代码,if __name__ == "__main__":
是什么意思?做?
# Threading example
import time, thread
def myfunction(string, sleeptime, lock, *args):
while True:
lock.acquire()
time.sleep(sleeptime)
lock.release()
time.sleep(sleeptime)
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))
最佳答案
简答
它是样板代码,可防止用户在无意中意外调用脚本。以下是脚本中省略守卫时的一些常见问题:
import my_script_without_a_name_eq_main_guard
),那么第二个脚本将触发第一个在导入时运行并使用第二个脚本的命令行参数。这几乎总是一个错误。__name__
, 接着__name__
的问题有何关联。我们在 Python 脚本中经常看到的检查。foo.py
的文件中.# Suppose this is foo.py.
print("before import")
import math
print("before functionA")
def functionA():
print("Function A")
print("before functionB")
def functionB():
print("Function B {}".format(math.sqrt(100)))
print("before __name__ guard")
if __name__ == '__main__':
functionA()
functionB()
print("after __name__ guard")
特殊变量__name__
多变的。python foo.py
解释器将分配硬编码字符串 "__main__"
到 __name__
变量,即# It's as if the interpreter inserts this at the top
# of your module when run as the main program.
__name__ = "__main__"
当您的模块被另一个人导入时 # Suppose this is in some other main program.
import foo
口译员将搜索您的 foo.py
文件(以及搜索其他一些变体),并且在执行该模块之前,它将分配名称 "foo"
从导入语句到 __name__
变量,即# It's as if the interpreter inserts this at the top
# of your module when it's imported from another module.
__name__ = "foo"
执行模块的代码"before import"
(不带引号)。math
模块并将其分配给名为 math
的变量.这相当于替换import math
具有以下内容(请注意,__import__
是 Python 中的一个低级函数,它接受一个字符串并触发实际导入):# Find and load a module given its string name, "math",
# then assign it to a local variable called math.
math = __import__("math")
"before functionA"
.def
块,创建一个函数对象,然后将该函数对象分配给一个名为 functionA
的变量."before functionB"
.def
块,创建另一个函数对象,然后将其分配给名为 functionB
的变量."before __name__ guard"
.__name__
确实设置为 "__main__"
并调用这两个函数,打印字符串 "Function A"
和 "Function B 10.0"
. __name__
将是 "foo"
,不是 "__main__"
,它将跳过 if
的正文陈述。 "after __name__ guard"
在这两种情况下。 # What gets printed if foo is the main program
before import
before functionA
before functionB
before __name__ guard
Function A
Function B 10.0
after __name__ guard
# What gets printed if foo is imported as a regular module
before import
before functionA
before functionB
before __name__ guard
after __name__ guard
为什么它以这种方式工作?.py
文件既可以被其他程序和/或模块用作模块,也可以作为主程序本身运行。例子:.py
工作。像您的脚本和运行特殊测试功能的文件。您不希望它仅仅因为它正在导入模块而尝试运行脚本。__name__
吗?检查块?答:这样做很奇怪,但语言不会阻止您。foo2.py
中.如果你说 python foo2.py
会发生什么在命令行上?为什么?# Suppose this is foo2.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def functionA():
print("a1")
from foo2 import functionB
print("a2")
functionB()
print("a3")
def functionB():
print("b")
print("t1")
if __name__ == "__main__":
print("m1")
functionA()
print("m2")
print("t2")
__name__
会发生什么入住 foo3.py
:# Suppose this is foo3.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def functionA():
print("a1")
from foo3 import functionB
print("a2")
functionB()
print("a3")
def functionB():
print("b")
print("t1")
print("m1")
functionA()
print("m2")
print("t2")
# Suppose this is in foo4.py
__name__ = "__main__"
def bar():
print("bar")
print("before __name__ guard")
if __name__ == "__main__":
bar()
print("after __name__ guard")
关于python - 如果 __name__ == "__main__": do? 会怎样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/419163/