3

Why is Python (3.8) throwing NameError when using class variables? The following works fine,

class Tester(object):
    # Number of measurements
    N = 6
    temp = [ 0. for x in range(N) ]

However when I try to use it as follows, it gives NameError.

class Tester(object):
    # Number of measurements
    N = 6
    temp = [[ 0. for x in range(N) ] for y in range(N) ]

It seems to be happening for the first N. So something like temp = [[ 0. for x in range(6) ] for y in range(N) ] works fine.

When I replace N by Tester.N, it gives NameError for Tester, which makes sense since the class is not defined.

How can I define the temp which is a 2-D list?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
mythealias
  • 350
  • 1
  • 9

1 Answers1

0

I'm not sure why your approach doesn't work. If you add it to the init it works:

class Tester(object):

    def __init__(self):
        # Number of measurements
        N = 6
        temp = [[ 0. for x in range(N) ] for y in range(N) ]
        print(temp)

tester = Tester()
Nathan
  • 3,558
  • 1
  • 18
  • 38