Python之“引用”

来源:互联网 发布:淘宝网首页开衫中长 编辑:程序博客网 时间:2024/04/29 15:11

Python中有类似C语言的数组名的特性,叫做“引用”,本质上是变量的内存地址。这就比较容易理解了。

# reference.pyprint 'SimpleAssigment'shoplist=['apple','mango','carrot','banana']mylist =shoplistdel shoplist[0]print mylist,'is mylist'print shoplist,"is my shoplist"mylist=shoplist[:] #equal to make a copy for shaoplist ,and my list is new allocated memory#这个部分就等于mylist新建了个副本存储shoplist,对mylist的操作不会影响shoplist del mylist[0]print mylist,'is mylist'print shoplist,"is my shoplist"

输出结果为:

SimpleAssigment['mango', 'carrot', 'banana'] is mylist['mango', 'carrot', 'banana'] is my shoplist['carrot', 'banana'] is mylist['mango', 'carrot', 'banana'] is my shoplistProcess finished with exit code 0




0 0