The problem:
class dummy(object)
size = 3
empty_array = [[None for y in range(size)] for x in range(size)]
I then want my code to generate something like (formated for readability)
empty_array[ [None, None, None],
[None, None, None],
[None, None, None] ]
Instead, it tells me the inner 'size'(for the 'y' iteration) doesn't exist as a variable(specifically: NameError: name 'size' is not defined
). This seems to imply that it can't find the outer global 'size', which is where I'm stuck.
Is there a way to generate this type of structure efficiently? Some way to keep the variable from needing to be declared in 2 separate spots?
My previous version is this, which did work fine but was a bit clunky and unintuitive for what I wanted to do.
empty_array = []
for x in range(size):
empty_array[x] = []
for y in range(size):
empty_array[x].append(None)
I need 'empty' values because my use case requires (x,y) style coordinates with both empty and 'stored' values, and I'd run into list-size errors if I just tried inserting into a list of empty lists, since they don't have any actual 'positions' to lock onto.
Annoyingly, if I simply declare the inner 'size' as a number, it works fine, so I know this type of expression works in theory, but I also need to be able to define 'size' as different numbers, so a fixed-value won't work for a final solution.
Edit: Narrowed the problem down, more context: This is in a class variable declaration, which seems to cause the simple solution of [[None]*size for _ range(size)]
to fail
this works:
size1 = 2
array = [[None]*size1 for x in range(size1)]
but this doesn't:
class dummy(object):
size2 = 3
array = [[None]*size2 for x in range(size2)]