Search code examples
pythondictionarydeep-copy

What happens if you deep-copy dictionary to itself?


I just saw some code that says data = copy.deepcopy(data).

  • Does this work?
  • What happens here?

Solution

  • It will allocate new memory for the data. Try run this snippet code

    import copy
    
    data = [1, 2, 3, 4]
    print(hex(id(data)))
    data = copy.deepcopy(data)
    print(hex(id(data)))
    

    Different memory address will be printed.