Python 政府可否发表评论;“全球代码”;也会被执行吗?

Python 政府可否发表评论;“全球代码”;也会被执行吗?,python,Python,我不知道为什么在注释第七行时输出不同。代码如下: #!/usr/bin/python import threading import time def loop(thread_name): if False: print "1111" #The print is only used to prove this code block indeed not excute global dict_test # The output

我不知道为什么在注释第七行时输出不同。代码如下:

#!/usr/bin/python
import threading
import time    
def loop(thread_name):      
    if False:
        print "1111"   #The print is only used to prove this code block indeed not excute 
        global dict_test   # The output will be different when commenting this line code
    else:
        dict_test = {}
    i = 0
    while i < 10:
        i+=1
        print  "thread %s %s" % (thread_name,id(dict_test))
        time.sleep(1)

t1=threading.Thread(target=loop,args=('1'))
t2=threading.Thread(target=loop,args=('2'))
t1.start()
t2.start()
t1.join()
t2.join()
#/usr/bin/python
导入线程
导入时间
def循环(线程名称):
如果为假:
打印“1111”#打印仅用于证明此代码块确实未执行
全局dict#u test#注释此行代码时,输出将不同
其他:
dict_测试={}
i=0
当我<10时:
i+=1
打印“线程%s%s”%(线程名称、id(dict测试))
时间。睡眠(1)
t1=threading.Thread(目标=循环,参数=('1'))
t2=threading.Thread(目标=循环,参数=('2'))
t1.start()
t2.start()
t1.join()
t2.join()
如果解释是无论匹配哪个条件,全局变量都是预编译的,那么下面的代码为什么会报告错误

#!/usr/bin/python
import threading
import time    
def loop(thread_name):        
    if False:
        print "1111"   #The print is only used to prove this code block indeed not excute 
        global dict_test   # The output will be different when commenting or uncommenting this line code 
    else:
#        dict_test = {}
        pass
    i = 0
    while i < 10:
        i+=1
        print  "thread %s %s" % (thread_name,id(dict_test))
        time.sleep(1)

t1=threading.Thread(target=loop,args=('1'))
t2=threading.Thread(target=loop,args=('2'))
t1.start()
t2.start()
t1.join()
t2.join()
#/usr/bin/python
导入线程
导入时间
def循环(线程名称):
如果为假:
打印“1111”#打印仅用于证明此代码块确实未执行
全局dict_test#注释或取消注释此行代码时,输出将不同
其他:
#dict_测试={}
通过
i=0
当我<10时:
i+=1
打印“线程%s%s”%(线程名称、id(dict测试))
时间。睡眠(1)
t1=threading.Thread(目标=循环,参数=('1'))
t2=threading.Thread(目标=循环,参数=('2'))
t1.start()
t2.start()
t1.join()
t2.join()

无论是否执行if语句,全局语句都适用。因此,当注释出全局语句时,dict_test的赋值适用于局部范围。

无论是否执行if语句,全局语句都适用。因此,在注释全局语句时,dict_test的赋值将应用于局部范围。

要解析变量名称,请执行以下操作:

  • 局部范围

  • 任何封闭函数的作用域

  • 全球范围

  • 内置的

()

如果此处未提及
和其他流量控制结构。因此,
if
内部的范围与外部的范围相同,因此现有变量
dict_test
是全局的,无论该块是否正在执行

这可能令人惊讶,但这就是它的定义

我的输出是

thread 1 50663904
thread 2 50667360
thread 1 50667360
thread 2 50667360
thread 1 50667360
thread 2 50667360
...

因此,最初,当两个线程同时启动时,这两个变量是独立的。从第二次迭代开始,它们都引用相同的全局变量。

要解析变量名称,Python搜索:

  • 局部范围

  • 任何封闭函数的作用域

  • 全球范围

  • 内置的

()

如果此处未提及
和其他流量控制结构。因此,
if
内部的范围与外部的范围相同,因此现有变量
dict_test
是全局的,无论该块是否正在执行

这可能令人惊讶,但这就是它的定义

我的输出是

thread 1 50663904
thread 2 50667360
thread 1 50667360
thread 2 50667360
thread 1 50667360
thread 2 50667360
...

因此,最初,当两个线程同时启动时,这两个变量是独立的。从第二次迭代开始,它们都引用相同的全局变量。

Nice quirk;甚至我也被它愚弄了。嗨,Alex,我的问题是我不知道为什么当注释dict_test={}或删除注释时,程序的输出会不同。你可以先在你的电脑上试试;甚至我也被它愚弄了。嗨,Alex,我的问题是我不知道为什么当注释dict_test={}或删除注释时,程序的输出会不同。你可以先在你的电脑上试试。嗨,托尔斯滕。谢谢你的回复,但我还是有点困惑。如果是这样,为什么我的第二个程序会报告错误(全局名称“dict_test”未定义)?我更正了我的解释,也许这次我没有漏掉要点。我对全局块有了进一步的理解。非常感谢。但是我的第二个程序在你的电脑上正常吗?。。。为什么我总是出错?…第二个程序没有在我的电脑上运行,因为你从来没有定义dict_测试。它必须被定义为全球性的。嗨,托尔斯滕。谢谢你的回复,但我还是有点困惑。如果是这样,为什么我的第二个程序会报告错误(全局名称“dict_test”未定义)?我更正了我的解释,也许这次我没有漏掉要点。我对全局块有了进一步的理解。非常感谢。但是我的第二个程序在你的电脑上正常吗?。。。为什么我总是出错?…第二个程序没有在我的电脑上运行,因为你从来没有定义dict_测试。它必须被定义为全球性的。