Python的参考
来源:互联网 发布:网络消费者服务热线 编辑:程序博客网 时间:2024/05/16 17:31
原文http://www.kuqin.com/abyteofpython_cn/ch09s06.html
参考
当你创建一个对象并给它赋一个变量的时候,这个变量仅仅 参考 那个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被称作名称到对象的绑定。
一般说来,你不需要担心这个,只是在参考上有些细微的效果需要你注意。这会通过下面这个例子加以说明。
#!/usr/bin/python# Filename: reference.pyprint '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', shoplistprint 'mylist is', mylist# notice that both shoplist and mylist both print the same list without# the 'apple' confirming that they point to the same objectprint 'Copy by making a full slice'mylist = shoplist[:] # make a copy by doing a full slicedel mylist[0] # remove first itemprint 'shoplist is', shoplistprint 'mylist is', mylist# notice that now the two lists are different
输出
$ python reference.py
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']
它如何工作
大多数解释已经在程序的注释中了。你需要记住的只是如果你想要复制一个列表或者类似的序列或者其他复杂的对象(不是如整数那样的简单 对象 ),那么你必须使用切片操作符来取得拷贝。如果你只是想要使用另一个变量名,两个名称都 参考 同一个对象,那么如果你不小心的话,可能会引来各种麻烦。
0 0
- Python的参考
- Python学习11:参考的使用
- 转几个Python的连接,自己参考
- Python科学计算方面的参考教程
- 3。Python的参考书籍以及视频
- python调用R的方式有用的几个参考文章
- python多线程参考博客
- python + gtk 参考网站
- python排序方法参考
- Python命名规范参考
- Python: 对象与参考
- Python练习题参考
- python学习:参考
- Python 编码风格参考
- Python参考文档
- python检测SQL注入的相关代码(参考lijiejie)
- apriori算法的代码,python实现,参考《机器学习实战》
- 基于Python编写的Windows服务程序参考代码
- go语言获取数组长度
- 博客新人求关注!
- AngularJS中使用HTML5手机摄像头拍照
- 如何逃出教育的死亡谷-Ken Robinson Ted英语演讲
- bzoj 1901 动态区间第k大 (树套树)
- Python的参考
- 【猫厂面试题】——-阿里常问面试题目汇总及答案(JAVA)
- leetcode-268-Missing Number
- 463. Island Perimeter
- 《C Primer Plus》读书笔记——数组和指针(1)
- 二叉树的字符串创建和遍历,求深度,叶子节点数
- 深入理解 Linux 2.6 的 initramfs 機制 (上)
- android EventBus的简单使用
- POJ - 1094 Sorting It All Out解题报告