[Python-bugs-list] [ python-Bugs-563148 ] built-in LIST operation has bug
noreply@sourceforge.net
noreply@sourceforge.net
Sat, 01 Jun 2002 18:25:32 -0700
Bugs item #563148, was opened at 2002-06-01 01:23
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=563148&group_id=5470
Category: Python Interpreter Core
Group: Python 2.2.1
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Tom Stanik (mytost)
Assigned to: Nobody/Anonymous (nobody)
Summary: built-in LIST operation has bug
Initial Comment:
example, to create a list of lists one can enter:
>>> d=[[0] * 4] * 5
>>> print d
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0,
0]]
if you want to change the first element in the first list:
>>> d[0][0]=1
>>> print d
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0,
0]] ***** INCORECT *****, all the first element changed
Alternatively, if you build the list manually:
>>> d=[0,0,0,0,0]
>>> d[0]=[0,0,0,0]
>>> d[1]=[0,0,0,0]
>>> d[2]=[0,0,0,0]
>>> d[3]=[0,0,0,0]
>>> print d
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0,
0]]
changing the first element in the first list yields the
correct result
>>> d[0][0]=1
>>> print d
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0,
0]] ***** CORRECT *****
The same list depending on hows it built yields a
different behavior
my version of python:
$ python -v
# /usr/local/lib/python2.2/site.pyc
matches /usr/local/lib/python2.2/site.py
import site # precompiled
from /usr/local/lib/python2.2/site.pyc
# /usr/local/lib/python2.2/os.pyc
matches /usr/local/lib/python2.2/os.py
import os # precompiled
from /usr/local/lib/python2.2/os.pyc
import posix # builtin
# /usr/local/lib/python2.2/posixpath.pyc
matches /usr/local/lib/python2.2/posixpath.py
import posixpath # precompiled
from /usr/local/lib/python2.2/posixpath.pyc
# /usr/local/lib/python2.2/stat.pyc
matches /usr/local/lib/python2.2/stat.py
import stat # precompiled
from /usr/local/lib/python2.2/stat.pyc
# /usr/local/lib/python2.2/UserDict.pyc
matches /usr/local/lib/python2.2/UserDict.py
import UserDict # precompiled
from /usr/local/lib/python2.2/UserDict.pyc
# /usr/local/lib/python2.2/copy_reg.pyc
matches /usr/local/lib/python2.2/copy_reg.py
import copy_reg # precompiled
from /usr/local/lib/python2.2/copy_reg.pyc
# /usr/local/lib/python2.2/types.pyc
matches /usr/local/lib/python2.2/types.py
import types # precompiled
from /usr/local/lib/python2.2/types.pyc
# /usr/local/lib/python2.2/__future__.pyc
matches /usr/local/lib/python2.2/__future__.py
import __future__ # precompiled
from /usr/local/lib/python2.2/__future__.pyc
Python 2.2.1 (#1, May 10 2002, 23:36:51)
[GCC 2.95.3 20010315 (release) [FreeBSD]] on freebsd4
Type "help", "copyright", "credits" or "license" for more
information.
dlopen("/usr/local/lib/python2.2/lib-dynload/readline.so",
2);
import readline # dynamically loaded
from /usr/local/lib/python2.2/lib-dynload/readline.so
>>>
----------------------------------------------------------------------
>Comment By: Tom Stanik (mytost)
Date: 2002-06-02 01:25
Message:
Logged In: YES
user_id=556456
Its also documented on p.64 of "Learning Python" by
O'Reilly.
After submitting the bug report the problem kept bothering me
and I had to understand what was happening. I dug through
the documentation and found the answer in an early chapter.
I appoligize for not being able to withdraw the bug-report
before you guys got to it.
----------------------------------------------------------------------
Comment By: Neal Norwitz (nnorwitz)
Date: 2002-06-01 15:05
Message:
Logged In: YES
user_id=33168
This is the expected behaviour.
See FAQ entry 4.50.
http://www.python.org/cgi-bin/faqw.py?req=show&file=faq04.050.htp
----------------------------------------------------------------------
Comment By: Michael Hudson (mwh)
Date: 2002-06-01 15:03
Message:
Logged In: YES
user_id=6656
Sigh. This is not a bug. You could try reading
http://starship.python.net/crew/mwh/hacks/objectthink.html
to gain a better understanding of what's going on.
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=563148&group_id=5470