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
9e8dbbcd
Commit
9e8dbbcd
authored
Feb 14, 2009
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add keyword argument support to itertools.count().
parent
8c20189c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
9 additions
and
8 deletions
+9
-8
Doc/library/itertools.rst
Doc/library/itertools.rst
+3
-2
Lib/test/test_itertools.py
Lib/test/test_itertools.py
+1
-0
Modules/itertoolsmodule.c
Modules/itertoolsmodule.c
+5
-6
No files found.
Doc/library/itertools.rst
View file @
9e8dbbcd
...
...
@@ -194,15 +194,16 @@ loops that truncate the stream.
.. versionadded:: 3.1
.. function:: count(
n
=0, step=1)
.. function:: count(
start
=0, step=1)
Make an iterator that returns evenly spaced values starting with *n*. Often
used as an argument to :func:`map` to generate consecutive data points.
Also, used with :func:`zip` 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(2.5, 0.5) -> 3.5 3.0 4.5 ...
n = start
while True:
yield n
n += step
...
...
Lib/test/test_itertools.py
View file @
9e8dbbcd
...
...
@@ -355,6 +355,7 @@ class TestBasicOps(unittest.TestCase):
self
.
assertEqual
(
lzip
(
'abc'
,
count
(
2
,
3
)),
[(
'a'
,
2
),
(
'b'
,
5
),
(
'c'
,
8
)])
self
.
assertEqual
(
lzip
(
'abc'
,
count
(
2
,
0
)),
[(
'a'
,
2
),
(
'b'
,
2
),
(
'c'
,
2
)])
self
.
assertEqual
(
lzip
(
'abc'
,
count
(
2
,
1
)),
[(
'a'
,
2
),
(
'b'
,
3
),
(
'c'
,
4
)])
self
.
assertEqual
(
lzip
(
'abc'
,
count
(
2
,
3
)),
[(
'a'
,
2
),
(
'b'
,
5
),
(
'c'
,
8
)])
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
)))
self
.
assertEqual
(
take
(
3
,
count
(
2
,
3.25
-
4j
)),
[
2
,
5.25
-
4j
,
8.5
-
8j
])
...
...
Modules/itertoolsmodule.c
View file @
9e8dbbcd
...
...
@@ -2916,11 +2916,10 @@ count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_ssize_t
cnt
=
0
;
PyObject
*
long_cnt
=
NULL
;
PyObject
*
long_step
=
NULL
;
static
char
*
kwlist
[]
=
{
"start"
,
"step"
,
0
};
if
(
type
==
&
count_type
&&
!
_PyArg_NoKeywords
(
"count()"
,
kwds
))
return
NULL
;
if
(
!
PyArg_UnpackTuple
(
args
,
"count"
,
0
,
2
,
&
long_cnt
,
&
long_step
))
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"|OO:count"
,
kwlist
,
&
long_cnt
,
&
long_step
))
return
NULL
;
if
(
long_cnt
!=
NULL
&&
!
PyNumber_Check
(
long_cnt
)
||
...
...
@@ -3027,10 +3026,10 @@ count_repr(countobject *lz)
}
PyDoc_STRVAR
(
count_doc
,
"count([
firstval
[, step]]) --> count object
\n
\
"count([
start
[, step]]) --> count object
\n
\
\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
\
def count(firstval=0, step=1):
\n
\
x = firstval
\n
\
...
...
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