I create a class in a file. declare some class variable A = 5
and another class variable B = A+1
.
When importing that class from another file, I get a NameError: name 'A' is not defined.
Is there a way around this or can/should class variables not depend on other class variables the way I am trying to do that?
Note that this NameError only happens when I import form another file. If i were to define the classe and create an instance of that class in the same file, everything runs smoothly.
file: file1.py
class foo:
A = 5
B = A+7
file: file2.py
from file1 import foo
I then run:
python file2.py
and get
NameError: name 'A' is not defined.
EDIT : The example above was too simplistic to reflect my actual problem. I was actually trying to define a class as such:
class foo:
A = 5
B = [i*A for A in range(3)]
The reasons why it doesn't work in Python 3 are to be found in the link in my answer.