Why is one class variable not defined in list comprehension but another is?
问题 I just read the answer to this question: Accessing class variables from a list comprehension in the class definition It helps me to understand why the following code results in NameError: name \'x\' is not defined : class A: x = 1 data = [0, 1, 2, 3] new_data = [i + x for i in data] print(new_data) The NameError occurs because x is not defined in the special scope for list comprehension. But I am unable to understand why the following code works without any error. class A: x = 1 data = [0, 1,