I think you're misunderstanding the meaning of a variable.
When you create your list "boardspace", you are creating it with the values that a1 et al contain at that time. Then you reassign the variable a1 et al to a different value.
The list does not contain a1 or any of the other variables, but lists of strings. That's all.
Edit:
Say you have something like this:
a = 1
b = 2
arr = [[a, b], [b, a]]
And you want a change to a to be reflected in both sub lists in arr, then you'll need to do one of two things:
- Convert these variables to objects
- Store indexes in
arr to a separate list (not as nice)
There are reasons for both, but since you'll likely want to do #1, here's an example:
class Tile:
def __init__(self, val):
self.val = val
def __repr__(self):
return self.val
def __str__(self):
return self.val
a = Tile(' ')
b = Tile(' ')
arr = [[a, b], [b, a]]
print arr # [[ , ], [ , ]]
a.val = '0'
print arr # [[0, ], [ , 0]]
I've defined a class with some magic functions:
__init__- Constructor
__str__- str(obj) is equivalent to obj.__str__()
__repr__- Outputs a representation of the object. In this case, we only have one value, so val is a sufficient representation. It also makes print arr work. See the last post here for more info
The change here is that we've created an object. Python passes everything by value, so when it passes the objects we created to the array, the reference is copied, but the underlying object is the same.
This means that any change to any of these objects will behave as you anticipated in your question.