Any other Python flaws?

Edward C. Jones edcjones at erols.com
Thu Jun 14 17:22:02 EDT 2001


jcm wrote:

> Edward C. Jones <edcjones at erols.com> wrote:
> 
> 
>>It follows from some basic concepts of Python that
>>     "array = ([0] * 10) * 10"
>>does not behave as usually intended. This is a well-known "dark 
>>corner" of Python; a place it is simply best to avoid.
>>
> 
> For this example, what would you say people would usually expect?
> 

Here is some correct code:

array = [[0] * 3] *3
print array
array[0][0] = 1
print array

The output is:

[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]

One might expect:

[[1, 0, 0], [0, 0, 0], [0, 0, 0]]





More information about the Python-list mailing list