-5

I'm recreating Conway's game of life in pygame, and I've been having an issue implementing the preset feature. When I'm assigning the preset array to the default array, and then assigning it back to the default array again, the preset is changed. I'm only editing the array "Initial_frame" but it is somehow changing the preset array.

Code: https://paste.pythondiscord.com/uvetekikot.py

  • I can't be sure from just the question text, but I think your `Initial_frame` array *is* the `default array`. Python will do it's utmost to not copy a data structure. Please read this post for more details. https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list . You can do something like: `inital_frame = preset_frame[:]` and make a slice-copy. – Kingsley Feb 27 '19 at 22:11
  • thanks this helped, but I had to use copy because they were complex. Thanks! – Skadoodlefap Feb 28 '19 at 19:39

1 Answers1

0

You are probably doing something like this:

a = [1 2 3 4 5]
b = a

a and b are really arrays, rather they are references to arrays. b=a means they both reference the same array.

You will need to copy the array instead of just copying the reference, like this:

b = a[:]
zmbq
  • 38,013
  • 14
  • 101
  • 171
  • `a` is not an [`array`](https://docs.python.org/3/library/array.html#array.array) but a [`list`](https://docs.python.org/3/library/stdtypes.html#list). – Matthias Feb 27 '19 at 20:32