为什么我的for循环列表中最后一项总会被覆盖的原因是什么?,for总会,1.如题,我真的是没办法


1.如题,我真的是没办法了,用尽各种方法list的最后一个数据都会覆盖前面的数据。
2.

class lotto_result_cl:    def __init__(self, index, spe_p):        lotto_result_cl.index = index        lotto_result_cl.spe = spe_pspe_nums_int =[47, 13, 43]spe_cl_list = []for i in range(len(spe_nums_int)):    spe_cl_list.append(lotto_result_cl(i+1, spe_nums_int[i]))for i in range(len(spe_cl_list)):    print spe_cl_list[i].index    print spe_cl_list[i].spe

3.结果:

343343343但是我要的结果是:147213343

内存分布:

<__main__.lotto_result_cl instance at 0x01724968><__main__.lotto_result_cl instance at 0x01724990><__main__.lotto_result_cl instance at 0x017249B8>

证明list里面全部对象不是引用了同一个对象

你这个类的init有问题,对之前创造的实例进行了修改,应为

class lotto_result_cl:

def __init__(self, index, spe_p):    self.index = index    self.spe = spe_p

即可

编橙之家文章,

评论关闭