python中定义变量和数组_Python中的线程和全局变量 - 数组和标准变量之间的区别?...
我目前嘗試使用線程編寫一個更大的python程序,并遇到了數組不必被聲明為全局的問題。Python中的線程和全局變量 - 數組和標準變量之間的區別?
import numpy as np
import threading
import time
import ctypes
import multiprocessing
import random
import os
def child1():
#global var
starttime = time.time()
for ite in range(10):
#data_np[ite] = time.time()-starttime
#print data_np[ite]
var += 1
print var
time.sleep(1)
def child2():
#global var
for ite in range(10):
#print data_np
print var
print "\n"
time.sleep(1)
os.system("clear")
data_np = np.zeros(shape=(10), dtype="float")
var = 0
thread1 = threading.Thread(target=child1)
thread2 = threading.Thread(target=child2)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
由于var未被聲明為全局變量,因此上述代碼將產生錯誤消息。 Unnotement
global var
產生一個可執行腳本,打印var的值兩次。 但是用numpy的陣列data_np,沒有聲明需要
global data_np
時。腳本將運行。 我不明白這種行為。有人可以對此作出解釋嗎?
2015-07-21
kM00n
總結
以上是生活随笔為你收集整理的python中定义变量和数组_Python中的线程和全局变量 - 数组和标准变量之间的区别?...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 工具_最全Mysql运维工具
- 下一篇: 字节跳动python面试_字节跳动的面试