Kaydet (Commit) a4038038 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Add keyword argument support to itertools.count().

üst 544c3e19
...@@ -200,20 +200,20 @@ loops that truncate the stream. ...@@ -200,20 +200,20 @@ loops that truncate the stream.
.. versionadded:: 2.7 .. versionadded:: 2.7
.. function:: count(n=0, step=1) .. function:: count(start=0, step=1)
Make an iterator that returns evenly spaced values starting with *n*. Often Make an iterator that returns evenly spaced values starting with *n*. Often
used as an argument to :func:`imap` to generate consecutive data points. used as an argument to :func:`imap` to generate consecutive data points.
Also, used with :func:`izip` to add sequence numbers. Equivalent to:: Also, used with :func:`izip` to add sequence numbers. Equivalent to::
def count(n=0, step=1): def count(start=0, step=1):
# count(10) --> 10 11 12 13 14 ... # count(10) --> 10 11 12 13 14 ...
# count(2.5, 0.5) -> 3.5 3.0 4.5 ... # count(2.5, 0.5) -> 3.5 3.0 4.5 ...
n = start
while True: while True:
yield n yield n
n += step n += step
.. versionchanged:: 2.7 .. versionchanged:: 2.7
added *step* argument and allowed non-integer arguments. added *step* argument and allowed non-integer arguments.
......
...@@ -347,6 +347,8 @@ class TestBasicOps(unittest.TestCase): ...@@ -347,6 +347,8 @@ class TestBasicOps(unittest.TestCase):
def test_count_with_stride(self): def test_count_with_stride(self):
self.assertEqual(zip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)]) self.assertEqual(zip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)])
self.assertEqual(zip('abc',count(start=2,step=3)),
[('a', 2), ('b', 5), ('c', 8)])
self.assertEqual(zip('abc',count(2,0)), [('a', 2), ('b', 2), ('c', 2)]) self.assertEqual(zip('abc',count(2,0)), [('a', 2), ('b', 2), ('c', 2)])
self.assertEqual(zip('abc',count(2,1)), [('a', 2), ('b', 3), ('c', 4)]) self.assertEqual(zip('abc',count(2,1)), [('a', 2), ('b', 3), ('c', 4)])
self.assertEqual(take(20, count(maxsize-15, 3)), take(20, range(maxsize-15, maxsize+100, 3))) self.assertEqual(take(20, count(maxsize-15, 3)), take(20, range(maxsize-15, maxsize+100, 3)))
......
...@@ -3233,11 +3233,10 @@ count_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -3233,11 +3233,10 @@ count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_ssize_t cnt = 0; Py_ssize_t cnt = 0;
PyObject *long_cnt = NULL; PyObject *long_cnt = NULL;
PyObject *long_step = NULL; PyObject *long_step = NULL;
static char *kwlist[] = {"start", "step", 0};
if (type == &count_type && !_PyArg_NoKeywords("count()", kwds)) if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:count",
return NULL; kwlist, &long_cnt, &long_step))
if (!PyArg_UnpackTuple(args, "count", 0, 2, &long_cnt, &long_step))
return NULL; return NULL;
if (long_cnt != NULL && !PyNumber_Check(long_cnt) || if (long_cnt != NULL && !PyNumber_Check(long_cnt) ||
...@@ -3353,10 +3352,10 @@ count_repr(countobject *lz) ...@@ -3353,10 +3352,10 @@ count_repr(countobject *lz)
} }
PyDoc_STRVAR(count_doc, PyDoc_STRVAR(count_doc,
"count([firstval[, step]]) --> count object\n\ "count([start[, step]]) --> count object\n\
\n\ \n\
Return a count object whose .next() method returns consecutive\n\ Return a count object whose .next() method returns consecutive\n\
integers starting from zero or, if specified, from firstval.\n\ integers starting from zero or, if specified, from start.\n\
If step is specified, counts by that interval. Equivalent to:\n\n\ If step is specified, counts by that interval. Equivalent to:\n\n\
def count(firstval=0, step=1):\n\ def count(firstval=0, step=1):\n\
x = firstval\n\ x = firstval\n\
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment