tcstory

Python 2.7.4 (default, Apr 19 2013, 18:32:33)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Person:                                #我自定义的类
...     def setname(self,name):
...             self.name=name
...
>>> list=[ ]                                            #建立一个空列表
>>> temp=Person()
>>> temp.setname(\'a\')
>>> list.append(temp)
>>> list[0].name
\'a\'                                                        #将temp追加到list中后,list[0].name 的值为‘a’
>>> temp.setname(\'b\')
>>> list.append(temp)
>>> list[0].name                                  #经过上两步后,居然list[0]和list[1] 的值都改变了
\'b\'
>>> list[1].name
\'b\'
>>> del temp                                     #删除之前存在的temp后,又重新定义一个,就没有问题了
>>> temp=Person()
>>> temp.setname(\'c\')
>>> list.append(temp)
>>> list[0].name
\'b\'
>>> list[1].name
\'b\'
>>> list[2].name
\'c\'
>>>

分类:

技术点:

相关文章:

  • 2021-10-31
  • 2021-07-02
  • 2021-12-24
  • 2021-08-06
  • 2020-01-09
  • 2020-07-16
  • 2021-06-30
  • 2021-11-27
猜你喜欢
  • 2021-11-08
  • 2021-11-01
  • 2021-10-19
  • 2019-12-03
  • 2019-03-22
  • 2021-11-02
  • 2021-08-10
相关资源
相似解决方案