zoukankan      html  css  js  c++  java
  • python 列表中插入自定义类的疑惑

    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'
    >>>

  • 相关阅读:
    Computer Browser服务自动停止
    Group By中Case分类统计
    C#判断网络状态
    Win7中VC6打开文件报错(转)
    SqlBulkCopy(大数据量拷贝)
    vc6开发ActiveX并发布全攻略(二)(转)
    VC6 Activex控件调试
    VC MessageBox
    常用基本AT指令
    WinForm自动重启
  • 原文地址:https://www.cnblogs.com/tcstory/p/3320615.html
Copyright © 2011-2022 走看看