Syntax: pointers versus value

Discussion in 'Python' started by Danny Castonguay, Jul 30, 2003.

  1. Simple question but I can't find the answer. Using an example:

    listA = [1 ,2]
    listB = listA
    listB.append(3)
    #listA now is [1,2,3]

    I want to avoid listA's value to change. Clearly, "listB = listA"
    creates the problem. listB and listA become two pointers to the same
    object. How then, do I duplicate the two objects; ie make a copy of the
    object that listA is pointing to and have listB point to that object.

    I'm new to python and I love it.

    Thank you,
    Danny Castonguay, Jul 30, 2003
    #1
    1. Advertising

  2. Danny Castonguay

    Tino Lange Guest

    On Wed, 30 Jul 2003 12:46:43 -0400, Danny Castonguay
    <> wrote:

    Hi!

    > Clearly, "listB = listA"
    >creates the problem. listB and listA become two pointers to the same
    >object.


    Exactly.

    > How then, do I duplicate the two objects; ie make a copy of the
    > object that listA is pointing to and have listB point to that object.


    Use:
    listB = listA[:]

    Cheers,

    Tino
    Tino Lange, Jul 30, 2003
    #2
    1. Advertising

  3. Tino Lange wrote:
    > Use:
    > listB = listA[:]


    --------------------------------------------------------------------
    In the example I have given, it does solve the problem. However, for
    some reason I don't understand, it doesn't work in the next example:

    def ps_of_missing_edges(initial_graph, missing_edges):
    num_m_e = len(missing_edges) #number of missing_edges
    num_e_pset = 2**num_m_e
    graphs = []
    for i in range(0,num_e_pset): #iteration will stop at 2^num-1
    temp_i = i #use temp_i to find i's bit values
    new_graph = initial_graph[:]
    print 'initial_graph is ' + str(initial_graph)
    for j in range (0,num_m_e):
    if temp_i%2 == 1:
    new_graph[missing_edges[j][0]-1].append(missing_edges[j][1])
    temp_i = temp_i >> 1
    graphs.append(new_graph)
    return graphs

    --------------------------------------------------------------

    the output when I call:
    ps_of_missing_edges([[2,3],[1],[1]], [[2,3],[3, 2]])
    is:
    initial_graph is [[2, 3], [1], [1]]
    initial_graph is [[2, 3], [1], [1]]
    initial_graph is [[2, 3], [1, 3], [1]]
    initial_graph is [[2, 3], [1, 3], [1, 2]]

    --------------------------------------------------------------
    Therefore, somehow initial_graph's value changes even though the
    assignment is new_graph = initial_graph[:]

    Assuming that the assignment does it's job, then I don't see how
    initial_graph's value changes.


    Thank you,
    Danny Castonguay, Jul 30, 2003
    #3
  4. Danny Castonguay

    Tino Lange Guest

    On Wed, 30 Jul 2003 13:46:29 -0400, Danny Castonguay
    <> wrote:

    >new_graph = initial_graph[:]
    > initial_graph is [[2, 3], [1], [1]]
    > initial_graph is [[2, 3], [1], [1]]
    > initial_graph is [[2, 3], [1, 3], [1]]
    > initial_graph is [[2, 3], [1, 3], [1, 2]]


    Hi!

    Now you have lists in lists. This is another situation.

    new_graph = initial_graph[:] will really make a copy of the outer list
    - but this outer list contains no values but once again pointers to
    other objects.

    Use the copy-module for nested copy things like that:
    http://www.python.org/doc/current/lib/module-copy.html

    Cheers,

    Tino
    Tino Lange, Jul 30, 2003
    #4
  5. Danny Castonguay

    John J. Lee Guest

    Danny Castonguay <> writes:

    > Simple question but I can't find the answer. Using an example:

    [...]
    > object. How then, do I duplicate the two objects; ie make a copy of
    > the object that listA is pointing to and have listB point to that
    > object.

    [...]

    http://www.python.org/doc/FAQ.html#4.38


    John
    John J. Lee, Jul 30, 2003
    #5
  6. Danny Castonguay

    John Hunter Guest

    >>>>> "Danny" == Danny Castonguay <> writes:

    Danny> I want to avoid listA's value to change.

    There are several ways

    listB = listA[:]

    listB = [x for x in listA]

    import copy
    listB = copy.copy(listA)
    listB = copy.deepcopy(listA)

    These don't all do the same thing, but all satisfy the example you
    posted, eg

    >>> listA = [1 ,2]
    >>> listB = listA[:]
    >>> listB.append(3)
    >>> listA

    [1, 2]

    So far so good, but consider this case

    >>> listA = [[],1]
    >>> listB = listA[:]
    >>> listB[0].append(12) # changing the list at listB[0]
    >>> listB

    [[12], 1]
    >>> listA

    [[12], 1]

    Sneaky, eh? So the 'listB = listA[:]' made a copy of the list, but
    not of the elements of the list. This is a shallow copy, and you
    should get the same behavior from copy.copy.

    Note that this is related to mutable and immutable objects in python.
    In the example above, I didn't replace the first item of the list, I
    changed it. Since both listA and listB contained the same object as
    their first element, changing one changes the other. If I had
    replaced that object in listB, nothing would have happened to listA

    >>> listA = [[],1]
    >>> listB = listA[:]
    >>> listB[0] = 12 # replacing the object, not changing it
    >>> listB

    [12, 1]
    >>> listA

    [[], 1]


    If you want to avoid this, use deepcopy.

    >>> import copy
    >>> listA = [[],1]
    >>> listB = copy.deepcopy(listA)
    >>> listB[0].append(12)
    >>> listB

    [[12], 1]
    >>> listA

    [[], 1]

    The behavior of mutable objects bites everyone a few times when they
    begin learning python, so welcome to the club!

    John Hunter
    John Hunter, Jul 30, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    1
    Views:
    6,863
    Scott M.
    Oct 11, 2003
  2. Russ

    script versus code versus ?

    Russ, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,470
  3. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    641
  4. Christoffer Sawicki
    Replies:
    5
    Views:
    235
    Christoffer Sawicki
    Sep 2, 2006
  5. Paul Butcher
    Replies:
    12
    Views:
    673
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page