• python数组类变量内的数组数据被同时修改了,该怎么解决
  • python
    5.00元

代码如下:

class Student:
   name = ''
   score = []
   def __init__(self):
       pass

def main():
   all = []
   all.append(Student())
   all.append(Student())
   all[0].name = 'YYY'
   all[1].name = 'XXX'
   all[0].score.append(75)
   all[1].score.append(85)
   print(all[0].score, all[1].score)

if __name__ == "__main__":
   main()

为什么print出来的是([75, 85], [75, 85]),数据被同时修改了。
请问如何让变量指向不同的地址?即我希望得到([75],[85]),谢谢!


  • 就几件    2020-05-13 10:49:41
  • 阅读 1743    收藏 0    回答 1
  • 邀请
  • 收藏
  • 分享
发送
登录 后发表评论
  • 51testing软件测试圈微信