Reputation: 18219
Here is a little script:
class Any(object):
def __init__(self,x):
self.x=x
l = [Any(2),Any(3),Any(7),Any(9),Any(10)]
print(len(l))
l2=[ind for ind in l]
l3=l
print(set(l2).difference(l3))
print(l2[1]==l[1])
print(l3[1]==l[1])
del l2[1]
print(len(l))
del l3[1]
print(len(l))
Why deleting an instance of Any
in l2
doesn't change l
, but deleting it in l3
changes l
although it seems not to have any difference between l2 and l3?
Thanks a lot!
Upvotes: 0
Views: 100
Reputation: 10003
l2
is a different object created from l
l3
refers to the same object as l
. So changing anything in l
or l3
will affect that object and therefore will affect l
and l3
.
Upvotes: 4
Reputation: 798456
Because:
>>> l is l2
False
>>> l is l3
True
Binding the reference twice makes both names refer to the same object.
Upvotes: 5