Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

array next pointer

0 views
Skip to first unread message

Anjanesh Lekshminarayanan

unread,
Mar 17, 2009, 7:12:51 AM3/17/09
to Python List
>>> a = ['cat','dog','elephant']
>>> a.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'next'
>>>
Is there something that imtates PHP's next() ? (http://php.net/next)

Andre Engels

unread,
Mar 17, 2009, 7:24:40 AM3/17/09
to Anjanesh Lekshminarayanan, Python List

>>> 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

Andre Engels

unread,
Mar 17, 2009, 7:26:13 AM3/17/09
to Anjanesh Lekshminarayanan, Python List
On Tue, Mar 17, 2009 at 12:24 PM, Andre Engels <andre...@gmail.com> wrote:
> On Tue, Mar 17, 2009 at 12:12 PM, Anjanesh Lekshminarayanan
> <ma...@anjanesh.net> wrote:
>>>>> a = ['cat','dog','elephant']
>>>>> a.next()
>> Traceback (most recent call last):
>>  File "<stdin>", line 1, in <module>
>> AttributeError: 'list' object has no attribute 'next'
>>>>>
>> Is there something that imtates PHP's next() ? (http://php.net/next)
>
>>>> b = a.__iter__()
>>>> b.next()
> 1
>>>> b.next()
> 2

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

andrew cooke

unread,
Mar 17, 2009, 7:35:45 AM3/17/09
to Andre Engels, pytho...@python.org
Andre Engels wrote:
[...]

>>>> b = a.__iter__()
>>>> b.next()
> 'cat'
>>>> b.next()
> 'dog'

not sure from what version, but certainly in 2.6 and on, you can improve
the syntax slightly:

>>> b = iter(a)
>>> b.next()

andrew


andrew cooke

unread,
Mar 17, 2009, 7:37:38 AM3/17/09
to Andre Engels, pytho...@python.org
Andre Engels wrote:
[...]
>>>> b = a.__iter__()
>>>> b.next()
> 'cat'
>>>> b.next()
> 'dog'

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


Tim Chase

unread,
Mar 17, 2009, 8:23:26 AM3/17/09
to andrew cooke, pytho...@python.org
> not sure from what version, but certainly in 2.6 and on, you can improve
> the syntax slightly:
>
>>>> b = iter(a)
>>>> b.next()

This syntax (also my preferred version) has been available since
at least 2.3

-tkc


Luis Zarrabeitia

unread,
Mar 17, 2009, 9:40:28 AM3/17/09
to pytho...@python.org

Quoting andrew cooke <and...@acooke.org>:

> 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

Benjamin Peterson

unread,
Mar 17, 2009, 2:23:38 PM3/17/09
to pytho...@python.org
Luis Zarrabeitia <kyrie <at> uh.cu> schrieb:

>
> 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!)

next() doesn't have a sentinel argument. It's iter() which does, and that's in
2.x also.


R. David Murray

unread,
Mar 17, 2009, 3:17:02 PM3/17/09
to pytho...@python.org

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

Luis Zarrabeitia

unread,
Mar 17, 2009, 6:04:25 PM3/17/09
to pytho...@python.org
On Tuesday 17 March 2009 03:17:02 pm R. David Murray wrote:
> > > (btw, I love the new sentinel argument for the next function in
> > > python3!)
> >
> > 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 ;)

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

Armin

unread,
Mar 16, 2009, 8:02:35 PM3/16/09
to pytho...@python.org
On Tuesday 17 March 2009 19:04:25 Luis Zarrabeitia wrote:
> On Tuesday 17 March 2009 03:17:02 pm R. David Murray wrote:
> > > > (btw, I love the new sentinel argument for the next function in
> > > > python3!)
> > >
> > > 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 ;)
>
> 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!

thanks,
--
Armin Moradi

Duncan Booth

unread,
Mar 18, 2009, 4:49:32 AM3/18/09
to
Armin <feng....@gmail.com> wrote:

> 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

Hrvoje Niksic

unread,
Mar 18, 2009, 5:10:03 AM3/18/09
to
Armin <feng....@gmail.com> writes:

>> 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

R. David Murray

unread,
Mar 18, 2009, 11:59:23 AM3/18/09
to pytho...@python.org

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.

0 new messages