python中定义变量和数组_Python中的线程和全局变量 - 数组和标准变量之间的区别?...
我目前嘗試使用線程編寫一個更大的python程序,并遇到了數(shù)組不必被聲明為全局的問題。Python中的線程和全局變量 - 數(shù)組和標(biāo)準(zhǔn)變量之間的區(qū)別?
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未被聲明為全局變量,因此上述代碼將產(chǎn)生錯誤消息。 Unnotement
global var
產(chǎn)生一個可執(zhí)行腳本,打印var的值兩次。 但是用numpy的陣列data_np,沒有聲明需要
global data_np
時。腳本將運(yùn)行。 我不明白這種行為。有人可以對此作出解釋嗎?
2015-07-21
kM00n
總結(jié)
以上是生活随笔為你收集整理的python中定义变量和数组_Python中的线程和全局变量 - 数组和标准变量之间的区别?...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 工具_最全Mysql运维工具
- 下一篇: 字节跳动python面试_字节跳动的面试