possible python bug here

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • manuel

    #1

    possible python bug here

    In the sample below, the foo function modify the b list,
    but I think it should modify only c, not b! It work
    correctly if if b is one dimension list instead two.

    def foo(aList):
    print "use foo..."
    aList[2][0] += .35
    aList[2][1] += .35
    aList[2][2] += .35

    p = [2.5,2.5,2.5]
    b = [p,p,p,p]

    c=b[:] #c is a totally new list cloned from b, it's not an alias!

    print "b = ",b[2]

    foo(c) #The argument is c, not b! Why b is modified too?

    print "b = ", b[2]# after foo(c), the list b now is different!
  • Peter Kleiweg

    #2
    Re: possible python bug here

    manuel schreef:
    [color=blue]
    > In the sample below, the foo function modify the b list,
    > but I think it should modify only c, not b! It work
    > correctly if if b is one dimension list instead two.
    >
    > def foo(aList):
    > print "use foo..."
    > aList[2][0] += .35
    > aList[2][1] += .35
    > aList[2][2] += .35
    >
    > p = [2.5,2.5,2.5]
    > b = [p,p,p,p]
    >
    > c=b[:] #c is a totally new list cloned from b, it's not an alias![/color]

    No it isn't. You didn't make a deep copy.



    --
    Peter Kleiweg L:NL,af,da,de,e n,ia,nds,no,sv, (fr,it) S:NL,de,en,(da, ia)
    info: http://www.let.rug.nl/~kleiweg/ls.html

    The Halloween Documents: http://www.opensource.org/halloween/

    Comment

    • manuel

      #3
      Re: possible python bug here

      Peter Kleiweg wrote:[color=blue]
      > manuel schreef:[color=green]
      >>c=b[:] #c is a totally new list cloned from b, it's not an alias![/color]
      >
      >
      > No it isn't. You didn't make a deep copy.[/color]

      Oh..thank you very much!

      Comment

      • Tuure Laurinolli

        #4
        Re: possible python bug here

        manuel wrote:
        [color=blue]
        > In the sample below, the foo function modify the b list,
        > but I think it should modify only c, not b! It work
        > correctly if if b is one dimension list instead two.
        >
        > def foo(aList):
        > print "use foo..."
        > aList[2][0] += .35
        > aList[2][1] += .35
        > aList[2][2] += .35
        >
        > p = [2.5,2.5,2.5][/color]
        Here you bind the name p to a list.[color=blue]
        > b = [p,p,p,p][/color]
        Here you bind the name b to a list, which has four references to list p[color=blue]
        >
        > c=b[:] #c is a totally new list cloned from b, it's not an alias![/color]
        Here you bind the name c to a list, which has four copies of the
        references to list p[color=blue]
        >
        > print "b = ",b[2][/color]
        Here you print the third reference to p of list b[color=blue]
        >
        > foo(c) #The argument is c, not b! Why b is modified too?[/color]
        Here you modify the third reference to p of list c[color=blue]
        >
        > print "b = ", b[2]# after foo(c), the list b now is different![/color]
        Here you print the third reference to p of list b, it's still just
        reference to list (originally bound to) p.

        Note that also the other references to the changed list (b[0:4], c[0:4],
        p) refer to the same changed list.

        Comment

        Working...