python list append命令 添加对象
当list中元素为类对象事,如果在追加类对象代码运行结果是5个4求大神指点。。附代码:globalUSRUSR=[]classUSRinfo(object):timed=...
当list中元素为类对象事,如果在追加类对象
代码运行结果是5个4
求大神指点。。
附代码:
global USR
USR=[]
class USRinfo(object):
timed=['00-00-00']
dataInfo={'ID':255,'havedevice':True,'hostID':1234567890,'hostCODE':'qwertyuiop'}
def dataBaseCreate():
for line in range(5):
Tmp=USRinfo()
Tmp.dataInfo['ID']=line
Tmp.dataInfo['hostID']=line
Tmp.dataInfo['hostCODE']=line
USR.append(Tmp)
#print USR[0].dataInfo['hostCODE']
dataBaseCreate()
for line in range(len(USR)):
print USR[line].dataInfo['hostCODE'] 展开
代码运行结果是5个4
求大神指点。。
附代码:
global USR
USR=[]
class USRinfo(object):
timed=['00-00-00']
dataInfo={'ID':255,'havedevice':True,'hostID':1234567890,'hostCODE':'qwertyuiop'}
def dataBaseCreate():
for line in range(5):
Tmp=USRinfo()
Tmp.dataInfo['ID']=line
Tmp.dataInfo['hostID']=line
Tmp.dataInfo['hostCODE']=line
USR.append(Tmp)
#print USR[0].dataInfo['hostCODE']
dataBaseCreate()
for line in range(len(USR)):
print USR[line].dataInfo['hostCODE'] 展开
1个回答
展开全部
参考我修改后的代码。简单的说,原来你定义的 datainfo 成员是类自身的成员,而不是每个类实例的成员。类自身成员是各个实例间共享的。
USR=[]
class USRinfo(object):
def __init__(self):
self.timed=['00-00-00']
self.dataInfo={'ID':255,'havedevice':True,'hostID':1234567890,'hostCODE':'qwertyuiop'}
def dataBaseCreate():
for line in range(5):
Tmp=USRinfo()
Tmp.dataInfo['ID']=line
Tmp.dataInfo['hostID']=line
Tmp.dataInfo['hostCODE']=line
USR.append(Tmp)
#print USR[0].dataInfo['hostCODE']
dataBaseCreate()
for line in range(len(USR)):
print USR[line].dataInfo['hostCODE']
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询