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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值