ProgrammingHardTCS NQT · Accenture
In Python, when is the outermost iterable of a generator expression such as (x for x in make()) evaluated?
- A. Immediately, when the generator expression is created
- B. Only when the first value is requested with next()
- C. After the whole expression finishes
- 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
- What is printed? python class Countdown: def init(self, n): self.n = n def iter(self): while self.n > 0: yield self.n self.n -= 2 c =…
- What is printed? python from itertools import permutations p = sorted(permutations('cab')) print([''.join(x) for x in p][3], len(p))
- What is printed? python print(() is (), [] is [], (1, 2) == (1, 2))
- A program must answer 10^5 range-sum queries over an array of 10^5 numbers in CPython. Which approach is fastest?
- What is printed? python from functools import lrucache class C: @lrucache(maxsize=None) def f(self, x): return x 2 a, b = C(), C() a.f(1)…
- What is printed? python class A: def init(self, v): self.v = v def eq(self, o): return isinstance(o, A) and self.v == o.v xs = [A(1)…