List of Lists of Object referencing same object in Python -


i'm new python , i'm not extremely familiar syntax , way things work exactly. it's possible i'm misunderstanding, can tell code line:

largeboard = [[board() in range(3)] j in range(3)]  

is creating 9 references same board object, rather 9 different board objects. how create 9 different board objects instead?

when run:

largeboard = [[board() in range(3)] j in range(3)]         x_or_o = 'x'       largeboard[1][0].board[0][0] = 'g' # each board has board inside list  in range(3):     j in range(3):         k in range(3):             l in range(3):                 print largeboard[i][j].board[k][l] 

i multiple 'g' made me think references same object.

you have reversed: are creating 9 independent board instances there. if had like

largeboard = [[board()] * 3] * 3 

then have single instance. root of common mistake many python newcomers make.

[x in range(3)] evaluates x once each i (3 times here) whereas [x] * 3 evaluates x once.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -