@@ -86,29 +86,38 @@ The following module functions all construct and return iterators. Some provide
8686streams of infinite length, so they should only be accessed by functions or
8787loops that truncate the stream.
8888
89- .. function :: accumulate(iterable[, func])
89+ .. function :: accumulate(iterable[, func, *, initial=None ])
9090
9191 Make an iterator that returns accumulated sums, or accumulated
9292 results of other binary functions (specified via the optional
93- *func * argument). If *func * is supplied, it should be a function
93+ *func * argument).
94+
95+ If *func * is supplied, it should be a function
9496 of two arguments. Elements of the input *iterable * may be any type
9597 that can be accepted as arguments to *func *. (For example, with
9698 the default operation of addition, elements may be any addable
9799 type including :class: `~decimal.Decimal ` or
98- :class: `~fractions.Fraction `.) If the input iterable is empty, the
99- output iterable will also be empty.
100+ :class: `~fractions.Fraction `.)
101+
102+ Usually, the number of elements output matches the input iterable.
103+ However, if the keyword argument *initial * is provided, the
104+ accumulation leads off with the *initial * value so that the output
105+ has one more element than the input iterable.
100106
101107 Roughly equivalent to::
102108
103- def accumulate(iterable, func=operator.add):
109+ def accumulate(iterable, func=operator.add, *, initial=None ):
104110 'Return running totals'
105111 # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
112+ # accumulate([1,2,3,4,5], initial=100) --> 100 101 103 106 110 115
106113 # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
107114 it = iter(iterable)
108- try:
109- total = next(it)
110- except StopIteration:
111- return
115+ total = initial
116+ if initial is None:
117+ try:
118+ total = next(it)
119+ except StopIteration:
120+ return
112121 yield total
113122 for element in it:
114123 total = func(total, element)
@@ -152,6 +161,9 @@ loops that truncate the stream.
152161 .. versionchanged :: 3.3
153162 Added the optional *func * parameter.
154163
164+ .. versionchanged :: 3.8
165+ Added the optional *initial * parameter.
166+
155167.. function :: chain(*iterables)
156168
157169 Make an iterator that returns elements from the first iterable until it is
0 commit comments