Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.7] Make code examples in Functional Programming HOWTO to be PEP 8 compliant. (GH-8646) #8705

Merged
merged 1 commit into from
Aug 7, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions Doc/howto/functional.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ for it.

You can experiment with the iteration interface manually:

>>> L = [1,2,3]
>>> L = [1, 2, 3]
>>> it = iter(L)
>>> it #doctest: +ELLIPSIS
<...iterator object at ...>
Expand Down Expand Up @@ -229,7 +229,7 @@ iterator. These two statements are equivalent::
Iterators can be materialized as lists or tuples by using the :func:`list` or
:func:`tuple` constructor functions:

>>> L = [1,2,3]
>>> L = [1, 2, 3]
>>> iterator = iter(L)
>>> t = tuple(iterator)
>>> t
Expand All @@ -238,10 +238,10 @@ Iterators can be materialized as lists or tuples by using the :func:`list` or
Sequence unpacking also supports iterators: if you know an iterator will return
N elements, you can unpack them into an N-tuple:

>>> L = [1,2,3]
>>> L = [1, 2, 3]
>>> iterator = iter(L)
>>> a,b,c = iterator
>>> a,b,c
>>> a, b, c = iterator
>>> a, b, c
(1, 2, 3)

Built-in functions such as :func:`max` and :func:`min` can take a single
Expand Down Expand Up @@ -411,7 +411,7 @@ lengths of all the sequences. If you have two lists of length 3, the output
list is 9 elements long:

>>> seq1 = 'abc'
>>> seq2 = (1,2,3)
>>> seq2 = (1, 2, 3)
>>> [(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),
Expand Down Expand Up @@ -479,7 +479,7 @@ Here's a sample usage of the ``generate_ints()`` generator:
File "stdin", line 2, in generate_ints
StopIteration

You could equally write ``for i in generate_ints(5)``, or ``a,b,c =
You could equally write ``for i in generate_ints(5)``, or ``a, b, c =
generate_ints(3)``.

Inside a generator function, ``return value`` causes ``StopIteration(value)``
Expand Down Expand Up @@ -695,17 +695,17 @@ truth values of an iterable's contents. :func:`any` returns ``True`` if any ele
in the iterable is a true value, and :func:`all` returns ``True`` if all of the
elements are true values:

>>> any([0,1,0])
>>> any([0, 1, 0])
True
>>> any([0,0,0])
>>> any([0, 0, 0])
False
>>> any([1,1,1])
>>> any([1, 1, 1])
True
>>> all([0,1,0])
>>> all([0, 1, 0])
False
>>> all([0,0,0])
>>> all([0, 0, 0])
False
>>> all([1,1,1])
>>> all([1, 1, 1])
True


Expand Down Expand Up @@ -764,7 +764,7 @@ which defaults to 0, and the interval between numbers, which defaults to 1::
a provided iterable and returns a new iterator that returns its elements from
first to last. The new iterator will repeat these elements infinitely. ::

itertools.cycle([1,2,3,4,5]) =>
itertools.cycle([1, 2, 3, 4, 5]) =>
1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...

:func:`itertools.repeat(elem, [n]) <itertools.repeat>` returns the provided
Expand Down Expand Up @@ -875,7 +875,7 @@ iterable's results. ::
iterators and returns only those elements of *data* for which the corresponding
element of *selectors* is true, stopping whenever either one is exhausted::

itertools.compress([1,2,3,4,5], [True, True, False, False, True]) =>
itertools.compress([1, 2, 3, 4, 5], [True, True, False, False, True]) =>
1, 2, 5


Expand Down Expand Up @@ -1035,7 +1035,7 @@ first calculation. ::
Traceback (most recent call last):
...
TypeError: reduce() of empty sequence with no initial value
>>> functools.reduce(operator.mul, [1,2,3], 1)
>>> functools.reduce(operator.mul, [1, 2, 3], 1)
6
>>> functools.reduce(operator.mul, [], 1)
1
Expand All @@ -1045,9 +1045,9 @@ elements of the iterable. This case is so common that there's a special
built-in called :func:`sum` to compute it:

>>> import functools, operator
>>> functools.reduce(operator.add, [1,2,3,4], 0)
>>> functools.reduce(operator.add, [1, 2, 3, 4], 0)
10
>>> sum([1,2,3,4])
>>> sum([1, 2, 3, 4])
10
>>> sum([])
0
Expand All @@ -1057,22 +1057,22 @@ write the obvious :keyword:`for` loop::

import functools
# Instead of:
product = functools.reduce(operator.mul, [1,2,3], 1)
product = functools.reduce(operator.mul, [1, 2, 3], 1)

# You can write:
product = 1
for i in [1,2,3]:
for i in [1, 2, 3]:
product *= i

A related function is :func:`itertools.accumulate(iterable, func=operator.add)
<itertools.accumulate>`. It performs the same calculation, but instead of
returning only the final result, :func:`accumulate` returns an iterator that
also yields each partial result::

itertools.accumulate([1,2,3,4,5]) =>
itertools.accumulate([1, 2, 3, 4, 5]) =>
1, 3, 6, 10, 15

itertools.accumulate([1,2,3,4,5], operator.mul) =>
itertools.accumulate([1, 2, 3, 4, 5], operator.mul) =>
1, 2, 6, 24, 120


Expand Down Expand Up @@ -1156,7 +1156,7 @@ But it would be best of all if I had simply used a ``for`` loop::

Or the :func:`sum` built-in and a generator expression::

total = sum(b for a,b in items)
total = sum(b for a, b in items)

Many uses of :func:`functools.reduce` are clearer when written as ``for`` loops.

Expand Down