>>> b = a.__iter__()
>>> b.next()
1
>>> b.next()
2
However, in many cases the more Pythonic way of working will be to use
a for-loop (which is like a foreach-loop in PHP and other languages).
--
André Engels, andre...@gmail.com
That should of course be:
>>> b = a.__iter__()
>>> b.next()
'cat'
>>> b.next()
'dog'
I had copied a bit too literal from my own attempt...
--
André Engels, andre...@gmail.com
not sure from what version, but certainly in 2.6 and on, you can improve
the syntax slightly:
>>> b = iter(a)
>>> b.next()
andrew
NOTE CORRECTION BELOW! IT'S next(b), not b.next() which doesn't exist in
Python 3 (if you need it, it's now b.__next__()). sorry.
not sure from what version, but certainly in 2.6 and on, you can improve
the syntax slightly:
>>> b = iter(a)
>>> next(b)
andrew
This syntax (also my preferred version) has been available since
at least 2.3
-tkc
> Andre Engels wrote:
> [...]
> >>>> b = a.__iter__()
>
> not sure from what version, but certainly in 2.6 and on, you can improve
> the syntax slightly:
> >>> b = iter(a)
> >>> b.next()
Indeed. Directly calling __special_methods__ should be avoided. That one is a
better idiom.
Works for python2.4 and 2.5 also.
In python3, this should be used instead:
>>> b = iter(a)
>>> c = next(b)
(btw, I love the new sentinel argument for the next function in python3!)
--
Luis Zarrabeitia
Facultad de Matemática y Computación, UH
http://profesores.matcom.uh.cu/~kyrie
Participe en Universidad 2010, del 8 al 12 de febrero de 2010
La Habana, Cuba
http://www.universidad2010.cu
next() doesn't have a sentinel argument. It's iter() which does, and that's in
2.x also.
But it does have a 'default' argument, and you can pass that
a sentinel, so it amounts to the same thing ;)
--
R. David Murray http://www.bitdance.com
Yep, that's what I meant, I forgot the parameter name.
--
Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.
http://profesores.matcom.uh.cu/~kyrie
Could you give an example of next() with a sentinel and describe its use case
please? I have a little trouble understanding what you guys mean!
thanks,
--
Armin Moradi
> Could you give an example of next() with a sentinel and describe its
> use case please? I have a little trouble understanding what you guys
> mean!
It means you don't have to worry about next() throwing StopIteration.
e.g.
>>> def pairs(sequence, padding=None):
sequence = iter(sequence)
for a in sequence:
b = next(sequence, padding)
yield a, b
>>> list(pairs('abcd'))
[('a', 'b'), ('c', 'd')]
>>> list(pairs('abcde'))
[('a', 'b'), ('c', 'd'), ('e', None)]
--
Duncan Booth http://kupuguy.blogspot.com
>> Yep, that's what I meant, I forgot the parameter name.
>
> Could you give an example of next() with a sentinel and describe its
> use case please? I have a little trouble understanding what you
> guys mean!
See the thread about reading the file in chunks. Instead of:
while True:
chunk = f.read(1024)
if chunk == '':
break
# your processing here
with sentinel iter, you can write:
for chunk in iter(lambda: f.read(1024), ''):
# your processing here
That's using the next parameter for its primary purpose: supplying a default.
The sentinel sub-case might be something like this:
SENTINEL = object()
while somecondition:
#do something
val = next(someiterator, SENTINEL)
if val is SENTINEL: break
#do something with val
In most cases you'd refactor code like that into a generator or something,
but there are cases where a pattern like the above can be useful. The
idea is that next will only return SENTINEL when the iterator is exhausted.
If you didn't use the SENTINEL, then StopIteration would be raised. So you
could write the above as:
try: val = next(someiterator)
except StopIteration: break
but the version using SENTINEL avoids having the interpreter do the work
of generating a traceback, and is IMO slightly prettier.
I'm sure there are other use cases, too, but probably not a huge number
of them. Certainly not as many as using the parameter as a default.