views:

225

answers:

2

I'm learning Python from a book, and I came across this example:

M = [[1,2,3],
     [4,5,6],
     [7,8,9]]

G = (sum(row) for row in M) # create a generator of row sums
next(G) # Run the iteration protocol

Since I'm an absolute beginner, and the author hasn't provided any explanation of the example or the next() function, I don't understand what the code is doing.

+18  A: 

The expression (sum(row) for row in M) creates what's called a generator. This generator will evaluate the expression (sum(row)) once for each row in M. However, the generator doesn't do anything yet, we've just set it up.

The statement next(G) actually runs the generator on M. So, if you run next(G) once, you'll get the sum of the first row. If you run it again, you'll get the sum of the second row, and so on.

>>> M = [[1,2,3],
...      [4,5,6],
...      [7,8,9]]
>>> 
>>> G = (sum(row) for row in M) # create a generator of row sums
>>> next(G) # Run the iteration protocol
6
>>> next(G)
15
>>> next(G)
24

See also:

jtbandes
+1: only thing which would improve this answer is a link to the docs (and official examples) on generators: http://docs.python.org/tutorial/classes.html#generators
Jarret Hardie
Thanks, good suggestion.
jtbandes
Also mention that 'next()' wasn't added as a builtin function until Python 2.7.
Andrew Dalke
It must have existed earlier, because my computer has 2.6.1 and it works.
jtbandes
It was included in 2.6: http://docs.python.org/2.6/library/functions.html#next
Roger Pate
+1  A: 

If you've come that far, then you should already know how a common for-in statement works.

The following statement:

for row in M: print row

would see M as a sequence of 3 rows (sub sequences) consisting of 3 items each, and iterate through M, outputting each row on the matrix:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

You knew that, well...

You can see Generators just as some syntactic sugar around for-in loops. Forget about the sum() call, and type something like this on IDLE:

G = (row for row in M)
print G
for a in G: print a

You see, the Generator cannot be directly represented as text, not just as a sequence can be. But, you can iterate through a Generator as if it were a sequence.

You'll find some big differences then, but the basics are that you can use a generator not to return just the value of each item in the sequence, but the result of any expression. In the tutorial's example, the expression is sum(row).

Try the following and see what happens:

G = ("("+str(row[2])+";"+str(row[1])+";"+str(row[0])+")" for row in M)
G.next()
G.next()
G.next()
Simón