is this a bug? (python 2.3)

Carsten Haese carsten at uniqsys.com
Fri Mar 9 15:43:53 EST 2007


On Fri, 2007-03-09 at 12:32 -0800, Sean McIlroy wrote:
> hi all
> 
> when i run this code in python 2.3
> 
> ######################  BEGIN CODE
> class warfare:
>     def __init__(self): self.pairs = [[0,0]]*2
>     def __str__(self): return str(self.pairs)
>     def setfirst (self,i,value): self.pairs[i][0] = value
>     def setsecond(self,i,value): self.pairs[i][1] = value
> 
> w = warfare()
> print w
> w.setfirst(0,'hello')
> print w
> ######################  END CODE
> 
> i get this result
> 
> ######################  BEGIN ACTUAL RESULT
> [[0, 0], [0, 0]]
> [['hello', 0], ['hello', 0]]
> ######################  END ACTUAL RESULT
> 
> instead of the expected result
> 
> ######################  BEGIN EXPECTED RESULT
> [[0, 0], [0, 0]]
> [['hello', 0], [0, 0]]
> ######################  END EXPECTED RESULT

http://www.python.org/doc/faq/programming/#how-do-i-create-a-multidimensional-list

-Carsten





More information about the Python-list mailing list