Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
35cbf162
Commit
35cbf162
authored
Oct 12, 2012
by
Ezio Melotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make doctests pass in the functional howto.
parent
45a101db
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
26 deletions
+17
-26
Doc/howto/functional.rst
Doc/howto/functional.rst
+17
-26
No files found.
Doc/howto/functional.rst
View file @
35cbf162
...
...
@@ -198,7 +198,7 @@ You can experiment with the iteration interface manually:
>>> L = [1,2,3]
>>> it = iter(L)
>>> it
>>> it
#doctest: +ELLIPSIS
<...iterator object at ...>
>>> it.__next__() # same as next(it)
1
...
...
@@ -267,15 +267,11 @@ sequence type, such as strings, will automatically support creation of an
iterator.
Calling :func:`iter` on a dictionary returns an iterator that will loop over the
dictionary's keys:
.. not a doctest since dict ordering varies across Pythons
::
dictionary's keys::
>>> 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}
>>> for key in m:
>>> for key in m:
#doctest: +SKIP
... print(key, m[key])
Mar 3
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
list is 9 elements long:
.. doctest::
:options: +NORMALIZE_WHITESPACE
>>> seq1 = 'abc'
>>> 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),
('b', 1), ('b', 2), ('b', 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:
.. testcode::
def generate_ints(N):
for i in range(N):
yield i
>>> def generate_ints(N):
... for i in range(N):
... yield i
Any function containing a :keyword:`yield` keyword is a generator function;
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:
>>> gen = generate_ints(3)
>>> gen
>>> gen
#doctest: +ELLIPSIS
<generator object generate_ints at ...>
>>> next(gen)
0
...
...
@@ -575,16 +566,16 @@ the internal counter.
And here's an example of changing the counter:
>>> it = counter(10)
>>> next(it)
>>> it = counter(10)
#doctest: +SKIP
>>> next(it)
#doctest: +SKIP
0
>>> next(it)
>>> next(it)
#doctest: +SKIP
1
>>> it.send(8)
>>> it.send(8)
#doctest: +SKIP
8
>>> next(it)
>>> next(it)
#doctest: +SKIP
9
>>> next(it)
>>> next(it)
#doctest: +SKIP
Traceback (most recent call last):
File "t.py", line 15, in ?
it.next()
...
...
@@ -687,11 +678,11 @@ constructed list's :meth:`~list.sort` method. ::
>>> import random
>>> # Generate 8 random numbers between [0, 10000)
>>> rand_list = random.sample(range(10000), 8)
>>> rand_list
>>> rand_list
#doctest: +SKIP
[769, 7953, 9828, 6431, 8442, 9878, 6213, 2207]
>>> sorted(rand_list)
>>> sorted(rand_list)
#doctest: +SKIP
[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]
(For a more detailed discussion of sorting, see the :ref:`sortinghowto`.)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment