O'Reilly logo
  • Chris Kang thinks this is interesting:

As a preview, though, you’ll find that in recent Pythons, comprehension syntax has been generalized for other roles: it’s not just for making lists today. For example, enclosing a comprehension in parentheses can also be used to create generators that produce results on demand. To illustrate, the sum built-in sums items in a sequence—in this example, summing all items in our matrix’s rows on request:

>>> G = (sum(row) for row in M)              # Cre...


Cover of Learning Python, 5th Edition


Looks like G is run one at a time for each call through next() for each row read from M.