0

I am designing a Python game played on a grid. This grid is represented by a list of a list - a nested list represents a row and each item in that list represents a "square".

Example:

t1 = tictactoe('p', True)
What size would you want your grid to be? 3
t1.current_grid
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
t2 = tictactoe('p', True)
What size would you want your grid to be? 3
t2.current_grid # now watch what happens
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9]]
t1.current_grid # gets even weirder
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9]]

I'm not using a global variable; I'm using just the variables within the class. Can anyone tell me why my optional parameter list keeps stacking up list from previous instance call of the class?

class Tictactoe:
      def __init__(self, p, interactive=False, current_grid=[]):

Then all it does is ask the user for grid size and append lists onto current_grid; I can't figure out why a different instance calls are stacking each other up.

Charlie
  • 21
  • 6

1 Answers1

1

Assigment in Python do not copy objects ! Please have a look at https://docs.python.org/2/library/copy.html You may want to use deepcopy to copy your list of lists.

MAC
  • 419
  • 3
  • 9