0

I'm writing the application and faced with interesting behavior of class initialization:

if __name__ == '__main__':
    class Types:
        VALUES = {i: f'value{i}' for i in range(1, 5)}

        MORE_VALUES = [(VALUES[i], f'more value +{i}') for i in range(1, 5)]

When I run this part of the code I see:

Traceback (most recent call last):
  File "/tutorial/main.py", line 3, in <module>
    class Types:
  File "/tutorial/main.py", line 6, in Types
    MORE_VALUES = [(VALUES[i], f'more value +{i}') for i in range(1, 5)]
  File "/tutorial/main.py", line 6, in <listcomp>
    MORE_VALUES = [(VALUES[i], f'more value +{i}') for i in range(1, 5)]
NameError: name 'VALUES' is not defined

How does the class perform variable initialization in this case and why it doesn't see the name 'VALUES'?

khelwood
  • 52,115
  • 13
  • 74
  • 94

0 Answers0