0
import copy

a = [12, 13, 15, 15, 14, 10]

d = copy.deepcopy(a)

print(id(d) == id(a))
print(id(d[0]) == id(a[0]))

e = copy.copy(a)

print(id(e) == id(a))
print(id(e[0]) == id(a[0]))

#outputs
False
True
False
True

I am using Python 3.4 and according to What exactly is the difference between shallow copy, deepcopy and normal assignment operation?, for d I should get outputs False and False, for e I should get outputs False and True.

Any ideas why this is happening?

After I changed a to [12, 13, 15, 15, 14, [10, 12]] and then checked id(d[5]) vs id(a[5]) the IDs were different. So, why aren't the IDs different for d[0] and a[0]?

Community
  • 1
  • 1
Akash B
  • 99
  • 8
  • Don't rely on `id` for immutables like integers or strings; http://stackoverflow.com/questions/3402679/identifying-objects-why-does-the-returned-value-from-id-change – Kos Apr 14 '15 at 19:55
  • 1
    With immutable objects, it doesn't really matter whether you get a new object or a reference to the old one, as you can't change it in-place either way. – jonrsharpe Apr 14 '15 at 19:56
  • Thanks for quick replies... And http://stackoverflow.com/questions/306313/pythons-is-operator-behaves-unexpectedly-with-integers helped as well.. Thanks – Akash B Apr 14 '15 at 20:06

0 Answers0