1

Executing this fails:

class Data:
    foo = 12
    bar = 42
    baz = [i * bar for i in range(foo)]

Executing this works:

foo = 12
bar = 42
baz = [i * bar for i in range(foo)]

I found this question which seems to be a more complicated version of essentially the same problem, but no clear answer to why the scope seems to change within a class + dict / list comprehension.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958

0 Answers0