4

I get NameError: name 'ID' is not defined when trying to access the class variable in a dict comprehension e.g. in class A. When referencing without a comprehension it works fine (see class B). So my question is, why does it throw a NameError in class A but not in class B?

class A:
    ID = "This is class A."

    test = {str(i): ID for i in range(5)}

class B:
    ID = "This is class B."

    test = f"{ID}"
ifloop
  • 43
  • 4
  • You question is not quite clear. can you explain what is `ID` ? And how is your `class B` working ? – Prashant Kumar Feb 24 '20 at 10:00
  • ID is just a string which is used as an identifier of the class. I want to use the ID class variable in a dict comprehension in the same class but that throws a NameError. In the case of class B without a comprehension, it works. So my question is, why does it throw a NameError in class A but not in class B? – ifloop Feb 24 '20 at 10:07
  • Can you post the full code ? There might be something wrong in the way you are using it. If you just copy 2 lines inside `class A` to python console it should works fine. – Prashant Kumar Feb 24 '20 at 10:09
  • Does this answer your question? https://stackoverflow.com/questions/13905741/accessing-class-variables-from-a-list-comprehension-in-the-class-definition – krismath Feb 24 '20 at 10:13
  • @krismath Thanks, that definitely does! – ifloop Feb 24 '20 at 10:19

1 Answers1

4

Note on Resolution of Names in Python:

Class definition blocks and arguments to exec() and eval() are special in the context of name resolution. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace. The namespace of the class definition becomes the attribute dictionary of the class. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope. This means that the following will fail.

You have declared a variable ID inside the class definition that makes it an class or static variable. So its scope is limited the to class block, and hence you can't access it inside the comprehensions.

you can read more about it at python docs

Consider the examples,

Example #1:

class A:
    ID = "This is class A."
    print(ID)

Now when you execute >>>A() the output will be This is class A which is totally fine because the scope of variable ID is limited to class A


Example #2:

class B:
    L = [ 1, 2, 3, 4, 5, 6, 7]
    print([i * 2 for i in L])

Now when you execute >>>B() the output will be [2, 4, 6, 8, 10, 12, 14] which is totally fine because the scope of list L is limited to class B


Example #3:

class C:
   L = [ 1, 2, 3, 4, 5, 6, 7]
   print([L[i] * 2 for i in range(7)])

Now executing >>>C() will raise a NameError stating that name L is not defined.

Shubham Sharma
  • 68,127
  • 6
  • 24
  • 53