Skip to content

Commit 9e8dbbc

Browse files
committed
Add keyword argument support to itertools.count().
1 parent 8c20189 commit 9e8dbbc

3 files changed

Lines changed: 9 additions & 8 deletions

File tree

Doc/library/itertools.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,16 @@ loops that truncate the stream.
194194
.. versionadded:: 3.1
195195

196196

197-
.. function:: count(n=0, step=1)
197+
.. function:: count(start=0, step=1)
198198

199199
Make an iterator that returns evenly spaced values starting with *n*. Often
200200
used as an argument to :func:`map` to generate consecutive data points.
201201
Also, used with :func:`zip` to add sequence numbers. Equivalent to::
202202

203-
def count(n=0, step=1):
203+
def count(start=0, step=1):
204204
# count(10) --> 10 11 12 13 14 ...
205205
# count(2.5, 0.5) -> 3.5 3.0 4.5 ...
206+
n = start
206207
while True:
207208
yield n
208209
n += step

Lib/test/test_itertools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ def test_count_with_stride(self):
355355
self.assertEqual(lzip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)])
356356
self.assertEqual(lzip('abc',count(2,0)), [('a', 2), ('b', 2), ('c', 2)])
357357
self.assertEqual(lzip('abc',count(2,1)), [('a', 2), ('b', 3), ('c', 4)])
358+
self.assertEqual(lzip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)])
358359
self.assertEqual(take(20, count(maxsize-15, 3)), take(20, range(maxsize-15, maxsize+100, 3)))
359360
self.assertEqual(take(20, count(-maxsize-15, 3)), take(20, range(-maxsize-15,-maxsize+100, 3)))
360361
self.assertEqual(take(3, count(2, 3.25-4j)), [2, 5.25-4j, 8.5-8j])

Modules/itertoolsmodule.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,11 +2916,10 @@ count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
29162916
Py_ssize_t cnt = 0;
29172917
PyObject *long_cnt = NULL;
29182918
PyObject *long_step = NULL;
2919+
static char *kwlist[] = {"start", "step", 0};
29192920

2920-
if (type == &count_type && !_PyArg_NoKeywords("count()", kwds))
2921-
return NULL;
2922-
2923-
if (!PyArg_UnpackTuple(args, "count", 0, 2, &long_cnt, &long_step))
2921+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:count",
2922+
kwlist, &long_cnt, &long_step))
29242923
return NULL;
29252924

29262925
if (long_cnt != NULL && !PyNumber_Check(long_cnt) ||
@@ -3027,10 +3026,10 @@ count_repr(countobject *lz)
30273026
}
30283027

30293028
PyDoc_STRVAR(count_doc,
3030-
"count([firstval[, step]]) --> count object\n\
3029+
"count([start[, step]]) --> count object\n\
30313030
\n\
30323031
Return a count object whose .__next__() method returns consecutive\n\
3033-
integers starting from zero or, if specified, from firstval.\n\
3032+
integers starting from zero or, if specified, from start.\n\
30343033
If step is specified, counts by that interval. Equivalent to:\n\n\
30353034
def count(firstval=0, step=1):\n\
30363035
x = firstval\n\

0 commit comments

Comments
 (0)