zoukankan      html  css  js  c++  java
  • 参考

    当你创建一个对象并给它赋一个变量的时候,这个变量仅仅 参考 那个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被称作名称到对象的绑定

     del删除的是变量,而不是数据

    这其实很简单。就是没有为它在内存里重新分配一个地址而已。

    #!/usr/bin/python
    # Filename: reference.py
    
    print 'Simple Assignment'
    shoplist = ['apple', 'mango', 'carrot', 'banana']
    mylist = shoplist # mylist is just another name pointing to the same object!
    
    del shoplist[0]
    
    print 'shoplist is', shoplist
    print 'mylist is', mylist
    # notice that both shoplist and mylist both print the same list without
    # the 'apple' confirming that they point to the same object
    
    print 'Copy by making a full slice'
    mylist = shoplist[:] # make a copy by doing a full slice
    del mylist[0] # remove first item
    
    print 'shoplist is', shoplist
    print 'mylist is', mylist
    # notice that now the two lists are different

    当对象赋值给变量时,其实并没有在内存里开辟新的地址。操作对象就是操作地址。所以MYLIST 跟着一起变化了。它们指向的是同一个地址。

     mylist = shoplist
     
     

    mylist = shoplist [:]  ,新开辟了一个地址。这就是新对象了。

    你需要记住的只是如果你想要复制一个列表或者类似的序列或者其他复杂的对象(不是如整数那样的简单 对象 ),那么你必须使用切片操作符来取得拷贝。

    如果你只是想要使用另一个变量名,两个名称都 参考 同一个对象,那么如果你不小心的话,可能会引来各种麻烦。

     这我感觉是目前来说,比较重要的一个部分了。
     
     
     
     
  • 相关阅读:
    立则存
    如何在UI控件上绑定一个变量
    欢迎大家使用www.eugene.org.cn访问我的blog
    属性与字段变量的区别
    在C#中联合Excel的Com编程入门
    我和我的土匪奶奶 全集 全 下载 txt
    大菲波数
    Hat's Fibonacci
    Fibonacci 取余,直接做超时
    How Many Fibs? 字符串转换处理
  • 原文地址:https://www.cnblogs.com/sakura3/p/8385370.html
Copyright © 2011-2022 走看看