0

I have this piece of code wherein I define two class variables:

class MyTestCase(unittest.TestCase):
    row = [1, 1, 1]
    new_row = [sum(row) for _ in range(3)]

This gives me the error message: new_row = [sum(row) for _ in range(3)] NameError: name 'row' is not defined

I worked around this by doing:

class MyTestCase(unittest.TestCase):
    row = [1, 1, 1]
    new_row = []
    for _ in range(3):
       new_row.append(sum(row))

This code ran successfully.

If row and new_row are not class variables the first way of defining new_row works fine. So why not as class variables? And why would the second piece of code run fine if the first one does not?

Konrad Rudolph
  • 506,650
  • 124
  • 909
  • 1,183
BHK
  • 1

0 Answers0