is dict.copy() a deep copy or a shallow copy

Discussion in 'Python' started by Alex, Sep 4, 2005.

  1. Alex

    Alex Guest

    Entering the following in the Python shell yields
    Help on method_descriptor:

    D.copy() -> a shallow copy of D

    Ok, I thought a dictionary copy is a shallow copy. Not knowing exactly
    what that meant I went to where
    I could read

    "...a deep copy is copy that contains the complete encapsulated data of
    the original object, allowing it to be used independently of the
    original object. In contrast, a shallow copy is a copy that may be
    associated to data shared by the original and the copy"

    Going back to Python shell I tested the following
    Hmm, this looks like a deep copy to me?? I also tried
    which looks like a shallow copy to me?? So my hypothesis is that E=D is
    a shallow copy while E=D.copy() is a deep copy.

    So is the documentation wrong when they claim that D.copy() returns a
    shallow copy of D, or did I misunderstand the difference between a deep
    and shallow copy?

    Alex, Sep 4, 2005
    1. Advertisements

  2. Alex

    Max Erickson Guest

    It is shallow, but strings are immutable so the difference is fairly
    Here, E and D are different names for the same object. There is no
    Sort of, some examples:

    Here d1 and d2 are copies. They can be independently changed to
    refer to different objects:
    {'a': 1, 'b': 4}

    Again, d3 and d4 are copies, but instead of changing the objects
    they refer to, we change the contents of the objects they refer to:
    {'c': [5], 'd': [6]}

    Both cases are shallow copies. In a deep copy, altering the contents
    of d3['c'] would have no impact on the contents of d4['c'].

    Max Erickson, Sep 4, 2005
    1. Advertisements

  3. Alex

    Alex Guest

    Thanks max,
    Now it's much clearer. I made the following experiment
    A shallow copy of an object is a copy of the first-level data members
    and if one of the members is a pointer, then only the pointer value is
    copied, not the structure pointed to by the pointer. The original
    object and its shallow copy share the memory space occupied by these
    A deep copy of an object is a copy of everything (including the
    structures pointe to by the pointers). The original object and its deep
    copy do not share any memory space.

    In #1 no pointers are involved so changing a value affects only D but
    not E.

    In #2 D['a'] and E['a'] are pointers that both point to the same memory
    space. Changing that memory space doesn't change the pointers

    In #3 a deepcopy makes sure that a copy is made not just of the
    pointers but also of the things pointed to.

    So the lesson to be learned is that if you make a copy of a dictionary
    whose values are pointers to other structures then a deepcopy prevents
    a coupling between a the original and the copy.

    Alex, Sep 5, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.