ProgrammingHardTCS NQT · Accenture

In Python, when is the outermost iterable of a generator expression such as (x for x in make()) evaluated?

  1. A. Immediately, when the generator expression is created
  2. B. Only when the first value is requested with next()
  3. C. After the whole expression finishes
  4. D. Once for every element produced

Correct answer: A. Immediately, when the generator expression is created

Only the outermost iterable is evaluated when the expression is created, as PEP 289 specifies. The conditions and element expressions run lazily.

More Python Programming questions

Exams that ask this topic