[issue8023] bug in s.append(x)

2010-02-26 Thread ughacks

New submission from ughacks ugha...@yahoo.com:

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]]

--
messages: 100141
nosy: ughacks
severity: normal
status: open
title: bug in s.append(x)
type: compile error
versions: Python 2.6

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue8023
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue8023] bug in s.append(x)

2010-02-26 Thread Mark Dickinson

Mark Dickinson dicki...@gmail.com added the comment:

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.

--
nosy: +mark.dickinson
resolution:  - invalid
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue8023
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue8023] bug in s.append(x)

2010-02-26 Thread ughacks

ughacks ugha...@yahoo.com added the comment:

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.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue8023
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue8023] bug in s.append(x)

2010-02-26 Thread Mark Dickinson

Mark Dickinson dicki...@gmail.com added the comment:

 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.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue8023
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com