diff --git a/advanced-iterators.html b/advanced-iterators.html index b7149e4..b7280ab 100644 --- a/advanced-iterators.html +++ b/advanced-iterators.html @@ -154,21 +154,33 @@ AssertionError
FIXME +
A generator expression is like a generator function without the function.
>>> unique_characters = {'E', 'D', 'M', 'O', 'N', 'S', 'R', 'Y'}
->>> gen = (ord(c) for c in unique_characters)
->>> gen
+>>> gen = (ord(c) for c in unique_characters) ①
+>>> gen ②
<generator object <genexpr> at 0x00BADC10>
->>> next(gen)
+>>> next(gen) ③
69
>>> next(gen)
68
->>> tuple(ord(c) for c in unique_characters)
+>>> tuple(ord(c) for c in unique_characters) ④
(69, 68, 77, 79, 78, 83, 82, 89)
+next(gen) returns the next value from the iterator.
+tuple(), list(), or set().
+FIXME +
Here’s another way to accomplish the same thing, using a generator function: + +
def ord_map(a_string):
+ for c in a_string:
+ yield ord(c)
+
+gen = ord_map(unique_characters)