code
is a class variable , and hence when accessing it you need to call - Test.code
, you cannot access them using code
.
Also, even if you do access them using Test.code
, it would still not work, because the value for class variables (default value) is calculated when the class is being defined, so when you try to access Test.code
, you would not be able to access Test
as it has not been created yet. Example of that -
>>> class Test:
... code = [ 1, 2, 3, 5, 6, 7, 8, 9, 10]
... code2d = [ [ Test.code[j*3 + i] for i in range(3) ] for j in range(3) ]
... def __init__(self):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in Test
File "<stdin>", line 3, in <listcomp>
File "<stdin>", line 3, in <listcomp>
NameError: name 'Test' is not defined
I am guessing when you put them in the __init__()
you are putting them as -
class Test:
def __init__(self):
code = [ 1, 2, 3, 5, 6, 7, 8, 9, 10]
code2d = [ [ code[j*3 + i] for i in range(3) ] for j in range(3) ]
This would work, because code
here is a local variable
and hence can be accesed by the other local variables in the __init__()
method , though they would not be accessible outside this function.
Maybe, you do not need them as class variables , if you want to have code
and code2d
for all instances of the class (objects of the class) , you should create instance variable as-
>>> class Test:
... def __init__(self):
... self.code = [ 1, 2, 3, 5, 6, 7, 8, 9, 10]
... self.code2d = [ [ self.code[j*3 + i] for i in range(3) ] for j in range(3) ]
If you really want code
and code2d
to be class variables , one way to do it would be to define code2d
outside the class Test
, example -
class Test:
code = [ 1, 2, 3, 5, 6, 7, 8, 9, 10]
def __init__(self):
pass
Test.code2d = [ [ Test.code[j*3 + i] for i in range(3) ] for j in range(3) ]