-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathds_using_reference.py
More file actions
36 lines (34 loc) · 1.25 KB
/
Copy pathds_using_reference.py
File metadata and controls
36 lines (34 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
拷贝和别名不是同一个东西
你要记住如果你希望创建一份诸如序列等复杂对象的副本(而非整数这种简单的对象 (Object)),
你必须使用切片操作来制作副本。如果你仅仅是将一个变量名赋予给另一个名 称,那么它们都将“查阅”同一个对象
"""
print('Simple Assignment')
shoplist = ['apple', 'mango', 'carrot', 'banana']
# mylist 只是指向同一对象的另一种名称
mylist = shoplist
# 我购买了第一项项目,所以我将其从列表中删除
del shoplist[0]
#del mylist[0]
print('shoplist is', shoplist)
print('mylist is', mylist)
# 注意到 shoplist 和 mylist 二者都
# 打印出了其中都没有 apple 的同样的列表,以此我们确认
# 它们指向的是同一个对象
print('Copy by making a full slice')
# 通过生成一份完整的切片制作一份列表的副本
mylist = shoplist[:]
# 删除第一个项目
del mylist[0]
print('shoplist is', shoplist)
print('mylist is', mylist)
# 注意到现在两份列表已出现不同
"""
运行结果:
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist is ['mango', 'carrot', 'banana']
mylist is ['carrot', 'banana']
"""