[issue23406] interning and list comprehension leads to unexpected behavior

2015-02-07 Thread Abraham Smith

New submission from Abraham Smith:

Some students were working on matrix routines for practice.

The following code:
>>> L = [ [0]*3 ]*3
>>> for i in range(3):
...for j in range(3):
...if i==j: L[i][j]=1

was expected to return
[[1,0,0],[0,1,0],[0,0,1]]
but it returned
[[1,1,1],[1,1,1],[1,1,1]]
because the list [0]*3 was being interned silently, so all three rows were the 
same memory!

To see this, I did
>>> map(id, L)
[139634871681464, 139634871681464, 139634871681464]

On the other hand
>>> M=[ [ 0 for i in range(3) ] for j in range(3) ]
does not intern:
>>> map(id, L)
[139634871631672, 139634871681608, 139634871681680]

so the above loop works as expected.
This is true in both python 2.7 and 3.4.

This is very confusing to users!

If this intern behavior with [0]*3 is intended, it should be documented more 
clearly, because this is something that new students of python might encounter 
right away when playing with the language's list methods.  I didn't see any 
reference to interning in the discussion of lists in the standard library 
reference. 

Moreover, I also could not find any reference to the automatic interning of 
mutable objects, such as lists.  Personally, I cannot see any reason to 
silently and automatically intern a mutable object; however, if this behavior 
is really desired, it should be documented.

--
assignee: docs@python
components: Documentation
messages: 235520
nosy: Abraham.Smith, docs@python
priority: normal
severity: normal
status: open
title: interning and list comprehension leads to unexpected behavior
versions: Python 2.7, Python 3.4

___
Python tracker 
<http://bugs.python.org/issue23406>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23406] interning and list comprehension leads to unexpected behavior

2015-02-07 Thread Abraham Smith

Abraham Smith added the comment:

(Obviously, there's a copy/paste mistake in the second case; it should read  
map(id, M).)

--

___
Python tracker 
<http://bugs.python.org/issue23406>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23406] interning and list comprehension leads to unexpected behavior

2015-02-07 Thread Abraham Smith

Abraham Smith added the comment:

Thanks for the helpful responses and correcting my misunderstanding.

Regarding improved documentation, I see now that the table at 
https://docs.python.org/2/library/stdtypes.html#id2 indeed says "shallow 
copies"; however, the footnote seems to bury the lede.   Perhaps the footnote 
should be expanded to either link to the FAQ entry or provide an abbreviated 
version of it.

The FAQ entry is actually very good, but I would guess that most readers (like 
me) skip the FAQs and jump straight to the library reference.Internet users 
have been trained for 20 years to believe that FAQs are full of useless, snarky 
answers to questions at a much shallower level, like "what do I do with a 
.tar.gz file?".  The fact that Python's FAQ is extremely well-written and 
helpful is a pleasant surprise, but a surprise none-the-less.

--
resolution: not a bug -> 

___
Python tracker 
<http://bugs.python.org/issue23406>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com