Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-12 Thread Terry Reedy
On 9/12/2011 12:55 PM, Ian Kelly wrote: On Sun, Sep 11, 2011 at 6:45 PM, Terry Reedy wrote: whereas, you are right, it breaks it noisily in the body. So Ian's claim that StopIteration must be caught to avoid silent termination is not true. Thanks for pointing out what I saw but did not cognize

Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-12 Thread Terry Reedy
On 9/12/2011 9:06 AM, Duncan Booth wrote: Terry Reedy wrote: The statement containing the explicit next(items) call can optionally be wrapped to explicitly handle the case of an empty iterable in whatever manner is desired. try: except StopIteration: raise ValueError("iterable ca

Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-12 Thread Ian Kelly
On Mon, Sep 12, 2011 at 10:55 AM, Ian Kelly wrote: > But you can't write the function under the assumption that it will > only be called from the function body.  The following is a slight > reorganization of your example that does exhibit the problem: s/function body/for-loop body/ -- http://mai

Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-12 Thread Ian Kelly
On Sun, Sep 11, 2011 at 6:45 PM, Terry Reedy wrote: > whereas, you are right, it breaks it noisily in the body. So Ian's claim > that StopIteration must be caught to avoid silent termination is not true. > Thanks for pointing out what I saw but did not cognize the full implication > of before. A b

Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-12 Thread Duncan Booth
Terry Reedy wrote: > The statement containing the explicit next(items) call can optionally be > wrapped to explicitly handle the case of an empty iterable in whatever > manner is desired. > > try: > > except StopIteration: > raise ValueError("iterable cannot be empty") > > Alterna

Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-11 Thread Terry Reedy
On 9/11/2011 6:41 PM, Chris Angelico wrote: On Mon, Sep 12, 2011 at 2:47 AM, Terry Reedy wrote: What you are saying is a) that the following code for title in ['amazinG', 'a helL of a fiGHT', '', 'igNordEd']: print(fix_title(title)) At least in Python 3.2, this isn't the case. StopItera

Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-11 Thread Chris Angelico
On Mon, Sep 12, 2011 at 2:47 AM, Terry Reedy wrote: > What you are saying is a) that the following code > > for title in ['amazinG', 'a helL of a fiGHT', '', 'igNordEd']: >    print(fix_title(title)) > At least in Python 3.2, this isn't the case. StopIteration breaks the loop only if it's raised

Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-11 Thread Ethan Furman
Terry Reedy wrote: On 9/11/2011 12:01 AM, Ian Kelly wrote: On Sat, Sep 10, 2011 at 1:36 PM, Terry Reedy wrote: The statement containing the explicit next(items) call can optionally be wrapped to explicitly handle the case of an empty iterable in whatever manner is desired. try: except St

Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-11 Thread Terry Reedy
On 9/11/2011 9:41 AM, Peter Otten wrote: Terry Reedy wrote: 3. Process the items of an iterable in pairs. items = iter(iterable) for first in items: second = next(items) This time, StopIteration is raised for an odd number of items. Catch and process as desired. One possibility i

Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-11 Thread Terry Reedy
On 9/11/2011 12:01 AM, Ian Kelly wrote: On Sat, Sep 10, 2011 at 1:36 PM, Terry Reedy wrote: The statement containing the explicit next(items) call can optionally be wrapped to explicitly handle the case of an empty iterable in whatever manner is desired. try: except StopIteration: rai

Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-11 Thread Peter Otten
Terry Reedy wrote: > 3. Process the items of an iterable in pairs. > > items = iter(iterable) > for first in items: > second = next(items) > > > This time, StopIteration is raised for an odd number of items. Catch and > process as desired. One possibility is to raise ValueError("Iter

Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-11 Thread Tim Chase
On 09/10/11 14:36, Terry Reedy wrote: 1. Process first item of an iterable separately. A traditional solution is a flag variable that is tested for each item. first = True for item in iterable: if first: first = False else: (I have seen code like this posted on thi

Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-10 Thread Ian Kelly
On Sat, Sep 10, 2011 at 1:36 PM, Terry Reedy wrote: > The statement containing the explicit next(items) call can optionally be > wrapped to explicitly handle the case of an empty iterable in whatever > manner is desired. > > try: >     > except StopIteration: >    raise ValueError("iterable cannot

Idioms combining 'next(items)' and 'for item in items:'

2011-09-10 Thread Terry Reedy
Python's iterator protocol for an iterator 'items' allows combinations of explicit "next(items)" calls with the implicit calls of a "for item in items:" loop. There are at least three situations in which this can be useful. (While the code posted here is not testable, being incomplete or having