An expression like this creates a list of integers:
>>> [0] * 2
[0, 0]
But an expression like this creates list of references to the list
named `foo':
>>> foo = [0, 0]
>>> baz = [foo] * 2
[foo, foo]
So, setting baz[0][0] = 1, is really setting foo[0] = 1. There is only
one instance of foo, but y
[EMAIL PROTECTED] wrote:
> Hello I found this very strange; is it a bug, is it a "feature", am I
> being naughty or what?
the repeat operator (*) creates a new list with references to the same
inner objects, so you end up with a list containing multiple references
to the same list. also see:
[EMAIL PROTECTED] wrote:
> Hello I found this very strange; is it a bug, is it a "feature", am I
> being naughty or what?
>
foo = [[0, 0], [0, 0]]
baz = [ [0]*2 ] * 2
>...
> Why on earth does foo and baz behave differently??
This is a frequently made mistake.
try also:
>>> bumble = [[0
Hello I found this very strange; is it a bug, is it a "feature", am I
being naughty or what?
>>> foo = [[0, 0], [0, 0]]
>>> baz = [ [0]*2 ] * 2
>>> foo
[[0, 0], [0, 0]]
>>> baz
[[0, 0], [0, 0]]
>>> foo[0][0]=1
>>> baz[0][0]=1
>>> foo
[[1, 0], [0, 0]]
>>> baz
[[1, 0], [1, 0]]
Why on earth does foo