is this a bug? (python 2.3)

S

Sean McIlroy

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[0] = value
def setsecond(self,i,value): self.pairs[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

is there a reasonable explanation for this behavior?

peace
stm
 
C

Carsten Haese

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[0] = value
def setsecond(self,i,value): self.pairs[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top