Where is the source code for Python itertools.islice?
I found itertoolsmodule.c, but can't find the function itertools.islice (maybe it's there, but I don't know much about C code).
I would like to understand what happens when I do:
itertools.islice(spamreader, rowStart, rowFinish+1)
You are looking at the old SVN repository; Python has since moved to mercurial. I'd look at he 2.7 version of itertoolsmodule.c
; the source has an islice
object.
When you call itertools.islice()
you create a new instance of that object, so islice_new
is called.
With 3 arguments, next
in the C struct is set to int(rowStart)
, stop
to int(rowFinish+1)
. step
is left as None
, it
is set to iter(spamreader)
. Most importantly, cnt
is set to 0
. Then the object is returned.
All the iteration work is done in islice_next()
, where it
is iterated over according to the next, stop and step values.
cnt
is incremented and next(it)
is called as needed to advance the object until cnt
is equal or greater to next
; after that, for each call to islice_next()
an item is yielded while cnt
and next
are incremented until stop
is reached.