4

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:

Boann
  • 48,794
  • 16
  • 117
  • 146
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
  • In comprehension, the values are all there in memory at the time of assignment. In generators, the next value isn't calculated until you need it next. This is useful for when each value requires you to have taken the last value and then perform runtime operations on it. – Rashid 'Lee' Ibrahim Aug 11 '20 at 15:09
  • 3
    See https://nedbatchelder.com/blog/201605/generator_comprehensions.html – superb rain Aug 11 '20 at 15:22
  • @superbrain Thanks!! That's super useful! So basically "Originally comprehension was part of the 'literal display' notion. GenExprs are not displays.". That's why they have different names – Riccardo Bucco Aug 11 '20 at 15:26
  • 1
    If it helps, I think of list/set comprehensions as generator expressions *inside* a literal, though it's not technically correct. – wjandrea Aug 11 '20 at 15:34

1 Answers1

5

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.

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • 1
    Thanks for you answer. However, I am aware of what they do, I'm just trying to understand why they have different names. Why aren't they called expressions, for example? – Riccardo Bucco Aug 11 '20 at 15:08
  • 6
    They have different names because they do different things. A generator simply generates the values. A comprehension contains a comprehensive set of those generated values. – Samwise Aug 11 '20 at 15:09
  • 2
    An "expression" is basically anything that has a value. Very generic programming term. – Samwise Aug 11 '20 at 15:15
  • 1
    @Samwise But comprehensions do return a value, don't they? That's why I did not understand why we can't call them expressions. – Riccardo Bucco Aug 11 '20 at 15:18
  • 2
    Not all generators are (single) expressions -- you say "generator expression" to refer to a single expression which acts as a generator, distinguishing it from a "generator function", which is composed of multiple expressions that create a generator. All comprehensions are expressions so "comprehension expression" would be redundant. – Samwise Aug 11 '20 at 15:34