Python中有类似C语言的数组名的特性,叫做“引用”,本质上是变量的内存地址。这就比较容易理解了。
# reference.py print 'SimpleAssigment' shoplist=['apple','mango','carrot','banana'] mylist =shoplist del 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 shoplist Process finished with exit code 0