zoukankan      html  css  js  c++  java
  • 数据结构之参考-对象与参考

    参考
    当你创建一个对象并给它赋一个变量的时候,这个变量仅仅 参考 那个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被称作名称到对象的绑定。
    一般说来,你不需要担心这个,只是在参考上有些细微的效果需要你注意。这会通过下面这个例子加以说明。
    对象与参考
    例9.6 对象与参考

    #!/usr/bin/python
    # Filename: reference.py
    print 'Simple Assignment'
    shoplist = ['apple', 'mango', 'carrot', 'banana']
    mylist=shoplist
    #del shoplist[0]
    del mylist[0]
    print 'shoplist is',shoplist
    print 'mylist is',mylist
    print'Copy by making a full slice'
    mylist=shoplist[:]
    del mylist[0]
    print 'shoplist is',shoplist
    print 'mylist is',mylist

    输出
    Simple Assignment
    shoplist is ['mango', 'carrot', 'banana']
    mylist is ['mango', 'carrot', 'banana']
    Copy by making a full slice
    shoplist is ['mango', 'carrot', 'banana']
    mylist is ['carrot', 'banana']
    它如何工作
    大多数解释已经在程序的注释中了。你需要记住的只是如果你想要复制一个列表或者类似的序列或者其他复杂的对象(不是如整数那样的简单 对象 ),那么你必须使用切片操作符来取得拷贝。如果你只是想要使用另一个变量名,两个名称都 参考 同一个对象,那么如果你不小心的话,可能会引来各种麻烦。
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    给Perl程序员的注释记住列表的赋值语句不创建拷贝。你得使用切片操作符来建立序列的拷贝。

  • 相关阅读:
    0X04-Twisted Teactor TCP Server
    0X03-SocketServer TCP服务器-Studying
    Python3---UDP服务器Studying
    Python3---学习TCP服务器
    (BFS)HDU 4784 Dinner Coming Soon
    (树状数组)HDU
    (状压dp)HDU 4778 Gems Fight!
    (二分)Codeforces Round #425 (Div. 2) C
    (LCA)Codeforces Round #425 (Div. 2) D
    (树的重心/DFS/构造)AtCoder Grand Contest 018 D
  • 原文地址:https://www.cnblogs.com/losbyday/p/5866505.html
Copyright © 2011-2022 走看看