Python中变量的绑定,或者说引用

 

 

print('The simple assignment')
shoppingList = ['chicken','mango','apple','banana']
myList = shoppingList

print('Before any action')
print('The shopping list is',shoppingList)
print('my list is',myList,'\n')

print('Del the first item from the shopping list')
del myList[0]
print('the shopping list is',shoppingList)
print('my list is',myList,'\n')

print('Copy and then del')
myList = shoppingList[:]
del myList[0]
print('the shopping list is',shoppingList)
print('my list is',myList,'\n')

#原对象改变,被原对象赋值的变量也随着改变
#被赋值的变量改变,原对象也改变
#变量指向对象在内存中的地址,所以删除了地址上的内容,对大家都有影响
#如果采用切片方式取得值的话,就没有影响

 2015/4/19 by Kerita

 

posted @ 2015-04-22 16:34  Kerita  阅读(768)  评论(0编辑  收藏  举报