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

 

相关文章:

  • 2021-06-22
  • 2022-12-23
  • 2021-12-18
  • 2022-12-23
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-04-03
  • 2021-11-07
  • 2021-10-01
  • 2021-07-03
  • 2022-12-23
相关资源
相似解决方案