This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: bug in s.append(x)
Type: compile error Stage:
Components: Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, ughacks
Priority: normal Keywords:

Created on 2010-02-26 08:55 by ughacks, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg100141 - (view) Author: Kang Sin Choi (ughacks) Date: 2010-02-26 08:55
Dear,

I am using
$ python -V
Python 2.6.4
on Ubuntu 9.10

I met a serious bug in s.append(x) operation. If I append a list into another list, there is a change of content. In the following code, [2,-2,0,0] is replaced with [-2,-2,0,0] after s.append(x) operaton.

------- begin of code ------

total=[]
for i in range(4):
    for j in range(i):
        root=[0,0,0,0]
        for k in [2,-2]:
         for l in [2,-2]:
          root[i]=k
          root[j]=l
          total.append(root)
          print root
print total

--------------- end of code -----

Result: each element is correctly generated
[2, 2, 0, 0]
[-2, 2, 0, 0]
[2, -2, 0, 0]
[-2, -2, 0, 0]
[2, 0, 2, 0]
[-2, 0, 2, 0]
[2, 0, -2, 0]
[-2, 0, -2, 0]
[0, 2, 2, 0]
[0, -2, 2, 0]
[0, 2, -2, 0]
[0, -2, -2, 0]
[2, 0, 0, 2]
[-2, 0, 0, 2]
[2, 0, 0, -2]
[-2, 0, 0, -2]
[0, 2, 0, 2]
[0, -2, 0, 2]
[0, 2, 0, -2]
[0, -2, 0, -2]
[0, 0, 2, 2]
[0, 0, -2, 2]
[0, 0, 2, -2]
[0, 0, -2, -2]

But the total list is wrong
[[-2, -2, 0, 0], [-2, -2, 0, 0], [-2, -2, 0, 0], [-2, -2, 0, 0], [-2, 0, -2, 0], [-2, 0, -2, 0], [-2, 0, -2, 0], [-2, 0, -2, 0], [0, -2, -2, 0], [0, -2, -2, 0], [0, -2, -2, 0], [0, -2, -2, 0], [-2, 0, 0, -2], [-2, 0, 0, -2], [-2, 0, 0, -2], [-2, 0, 0, -2], [0, -2, 0, -2], [0, -2, 0, -2], [0, -2, 0, -2], [0, -2, 0, -2], [0, 0, -2, -2], [0, 0, -2, -2], [0, 0, -2, -2], [0, 0, -2, -2]]
msg100142 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-02-26 09:18
This is a bug in your code, rather than in Python.

A simpler example, for the purposes of explanation:

>>> root, total = [0], []
>>> total.append(root)
>>> total  # good so far
[[0]]
>>> root[0] = 1   # modify root
>>> total  # note that total changes here!
[[1]]
>>> total.append(root)
>>> total
[[1], [1]]

In effect, total contains two references to the same list.  Modify that list, and you'll see those changes reflected in `total` as well.
msg100145 - (view) Author: Kang Sin Choi (ughacks) Date: 2010-02-26 10:29
Thank you for kind explanation.

So s.append(x) just copies the pointer, not the actual value. It is a little tricky to know that.
msg100148 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-02-26 11:13
> So s.append(x) just copies the pointer, not the actual value.

Yes, that's a reasonable way to think about it (though the term 'reference' seems to more popular than 'pointer' in this context).  It matches the implementation, too: internally, a list is represented as a resizable array of pointers to objects.  (For CPython, anyway;  alternative implementations of Python might differ.)

Implicit object copies are rare in Python.

comp.lang.python is a good place to ask questions about Python's object model.
History
Date User Action Args
2022-04-11 14:56:58adminsetgithub: 52271
2010-02-26 11:13:36mark.dickinsonsetmessages: + msg100148
2010-02-26 10:29:56ughackssetmessages: + msg100145
2010-02-26 09:18:20mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg100142

resolution: not a bug
2010-02-26 08:55:31ughackscreate