Make doctests pass in the functional howto.
This commit is contained in:
parent
45a101db1d
commit
35cbf16202
@ -198,7 +198,7 @@ You can experiment with the iteration interface manually:
|
|||||||
|
|
||||||
>>> L = [1,2,3]
|
>>> L = [1,2,3]
|
||||||
>>> it = iter(L)
|
>>> it = iter(L)
|
||||||
>>> it
|
>>> it #doctest: +ELLIPSIS
|
||||||
<...iterator object at ...>
|
<...iterator object at ...>
|
||||||
>>> it.__next__() # same as next(it)
|
>>> it.__next__() # same as next(it)
|
||||||
1
|
1
|
||||||
@ -267,15 +267,11 @@ sequence type, such as strings, will automatically support creation of an
|
|||||||
iterator.
|
iterator.
|
||||||
|
|
||||||
Calling :func:`iter` on a dictionary returns an iterator that will loop over the
|
Calling :func:`iter` on a dictionary returns an iterator that will loop over the
|
||||||
dictionary's keys:
|
dictionary's keys::
|
||||||
|
|
||||||
.. not a doctest since dict ordering varies across Pythons
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
>>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
|
>>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
|
||||||
... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
|
... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
|
||||||
>>> for key in m:
|
>>> for key in m: #doctest: +SKIP
|
||||||
... print(key, m[key])
|
... print(key, m[key])
|
||||||
Mar 3
|
Mar 3
|
||||||
Feb 2
|
Feb 2
|
||||||
@ -410,12 +406,9 @@ clauses, the length of the resulting output will be equal to the product of the
|
|||||||
lengths of all the sequences. If you have two lists of length 3, the output
|
lengths of all the sequences. If you have two lists of length 3, the output
|
||||||
list is 9 elements long:
|
list is 9 elements long:
|
||||||
|
|
||||||
.. doctest::
|
|
||||||
:options: +NORMALIZE_WHITESPACE
|
|
||||||
|
|
||||||
>>> seq1 = 'abc'
|
>>> seq1 = 'abc'
|
||||||
>>> seq2 = (1,2,3)
|
>>> seq2 = (1,2,3)
|
||||||
>>> [(x, y) for x in seq1 for y in seq2]
|
>>> [(x, y) for x in seq1 for y in seq2] #doctest: +NORMALIZE_WHITESPACE
|
||||||
[('a', 1), ('a', 2), ('a', 3),
|
[('a', 1), ('a', 2), ('a', 3),
|
||||||
('b', 1), ('b', 2), ('b', 3),
|
('b', 1), ('b', 2), ('b', 3),
|
||||||
('c', 1), ('c', 2), ('c', 3)]
|
('c', 1), ('c', 2), ('c', 3)]
|
||||||
@ -448,11 +441,9 @@ is what generators provide; they can be thought of as resumable functions.
|
|||||||
|
|
||||||
Here's the simplest example of a generator function:
|
Here's the simplest example of a generator function:
|
||||||
|
|
||||||
.. testcode::
|
>>> def generate_ints(N):
|
||||||
|
... for i in range(N):
|
||||||
def generate_ints(N):
|
... yield i
|
||||||
for i in range(N):
|
|
||||||
yield i
|
|
||||||
|
|
||||||
Any function containing a :keyword:`yield` keyword is a generator function;
|
Any function containing a :keyword:`yield` keyword is a generator function;
|
||||||
this is detected by Python's :term:`bytecode` compiler which compiles the
|
this is detected by Python's :term:`bytecode` compiler which compiles the
|
||||||
@ -470,7 +461,7 @@ executing.
|
|||||||
Here's a sample usage of the ``generate_ints()`` generator:
|
Here's a sample usage of the ``generate_ints()`` generator:
|
||||||
|
|
||||||
>>> gen = generate_ints(3)
|
>>> gen = generate_ints(3)
|
||||||
>>> gen
|
>>> gen #doctest: +ELLIPSIS
|
||||||
<generator object generate_ints at ...>
|
<generator object generate_ints at ...>
|
||||||
>>> next(gen)
|
>>> next(gen)
|
||||||
0
|
0
|
||||||
@ -575,16 +566,16 @@ the internal counter.
|
|||||||
|
|
||||||
And here's an example of changing the counter:
|
And here's an example of changing the counter:
|
||||||
|
|
||||||
>>> it = counter(10)
|
>>> it = counter(10) #doctest: +SKIP
|
||||||
>>> next(it)
|
>>> next(it) #doctest: +SKIP
|
||||||
0
|
0
|
||||||
>>> next(it)
|
>>> next(it) #doctest: +SKIP
|
||||||
1
|
1
|
||||||
>>> it.send(8)
|
>>> it.send(8) #doctest: +SKIP
|
||||||
8
|
8
|
||||||
>>> next(it)
|
>>> next(it) #doctest: +SKIP
|
||||||
9
|
9
|
||||||
>>> next(it)
|
>>> next(it) #doctest: +SKIP
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
File "t.py", line 15, in ?
|
File "t.py", line 15, in ?
|
||||||
it.next()
|
it.next()
|
||||||
@ -687,11 +678,11 @@ constructed list's :meth:`~list.sort` method. ::
|
|||||||
>>> import random
|
>>> import random
|
||||||
>>> # Generate 8 random numbers between [0, 10000)
|
>>> # Generate 8 random numbers between [0, 10000)
|
||||||
>>> rand_list = random.sample(range(10000), 8)
|
>>> rand_list = random.sample(range(10000), 8)
|
||||||
>>> rand_list
|
>>> rand_list #doctest: +SKIP
|
||||||
[769, 7953, 9828, 6431, 8442, 9878, 6213, 2207]
|
[769, 7953, 9828, 6431, 8442, 9878, 6213, 2207]
|
||||||
>>> sorted(rand_list)
|
>>> sorted(rand_list) #doctest: +SKIP
|
||||||
[769, 2207, 6213, 6431, 7953, 8442, 9828, 9878]
|
[769, 2207, 6213, 6431, 7953, 8442, 9828, 9878]
|
||||||
>>> sorted(rand_list, reverse=True)
|
>>> sorted(rand_list, reverse=True) #doctest: +SKIP
|
||||||
[9878, 9828, 8442, 7953, 6431, 6213, 2207, 769]
|
[9878, 9828, 8442, 7953, 6431, 6213, 2207, 769]
|
||||||
|
|
||||||
(For a more detailed discussion of sorting, see the :ref:`sortinghowto`.)
|
(For a more detailed discussion of sorting, see the :ref:`sortinghowto`.)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user