怎么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))
Run Code Online (Sandbox Code Playgroud) 我已经看了很多关于这个的文章
if __name__ == '__main__'
Run Code Online (Sandbox Code Playgroud)
但没有得到..我将与您分享代码,请您简要介绍一下。
我创建了一个文件“ ab.py”
def a():
print('A function in ab file');
a()
Run Code Online (Sandbox Code Playgroud)
第二个文件是“ xy.py”
import ab
def b():
print('b function')
def x(): print ('s');
x()
if __name__ == "__main__" :b()
Run Code Online (Sandbox Code Playgroud)
当我执行此代码时,此输出即将到来
A function in ab file
s
b function
Run Code Online (Sandbox Code Playgroud)
现在,我想知道这是什么意思,实际上是在做什么代码,我们为什么要实现它?没有它,我们的代码也可以工作
if __name__ == "__main__" :b()
Run Code Online (Sandbox Code Playgroud)