本帖最后由 jackz007 于 2022-11-8 18:39 编辑 myteacher = '小甲鱼' # myteacher 指向 '小甲鱼'
yourteacher = myteacher # yourteacher 也指向 '小甲鱼'
yourteacher = '黑夜' # yourteacher 指向 '黑夜'
print(myteacher) # myteacher 依然指向 '小甲鱼'
my_foods = ['pizza','falafel','carrot cake'] # my_foods 指向 ['pizza','falafel','carrot cake']
friend_foods = my_foods # friend_foods 也指向 ['pizza','falafel','carrot cake']
my_foods.append('cannoli') # ['pizza','falafel','carrot cake'] 添加新元素 'cannoli'
friend_foods.append('ice cream') # ['pizza','falafel','carrot cake' , 'cannoli'] 添加新元素 'ice cream'
print(my_foods) # ['pizza','falafel','carrot cake' , 'cannoli' , 'ice cream']
print(friend_foods) # ['pizza','falafel','carrot cake' , 'cannoli' , 'ice cream']
变量名只是贴在对象上的一个标签,只是访问对象的一个途径而已,一个对象可以同时有很多个标签,就像本例,多个变量同时指向同一个对象。例2中可修改对象改变,不影响贴在对象上的标签。 |