Why are generator expressions called that?

Viewed 91

On one hand we have

  • List comprehension [x for x in range(10)]
  • Set comprehension {x for x in range(10)}
  • Dict comprehension {x: x for x in range(10)}

On the other we have

  • Generator expression (x for x in range(10))

Why are the first three expressions called "comprehensions", while the last one is called "expression"? They are represented almost in the same way, and I guess they also work in a very similar way. Is there any subtle reason behind it? It's just for the sake of curiosity.

References:

1 Answers

Comprehensions produce new objects that consume the internal for loop immediately. They are "finished" once they run.

Generators are lazily evaluated - they return immediately, and return an object which will yield individual values later.

Related