a = [1,2,3,4,5]
b = a[1]
print id(a[1],b) # out put shows same id.hence both represent same object.
del a[1] # deleting a[1],both a[1],b have same id,hence both are aliases
print a # output: [1,3,4,5]
print b # output: 2
Both b,a[1] have same id but deleting one isn't effecting the other.Python reference states that 'del' on a subscription deletes the actual object,not the name object binding
. Output: [1,3,4,5] proves this statement.But how is it possible that 'b' remains unaffected when both a[0]
and b
have same id.
Edit: The part 'del' on a subscription deletes the actual object,not the name object binding
is not true.The reverse is true. 'del' actually removes the name,object bindings.In case of 'del' on subscription (eg. del a[1]) removes object 2 from the list object and also removes the current a[1]
binding to 2
and makes a[1]
bind to 3
instead. Subsequent indexes follow the pattern.