Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Dangerous behavior of list(generator)

Reply
Thread Tools

Re: Dangerous behavior of list(generator)

 
 
exarkun@twistedmatrix.com
Guest
Posts: n/a
 
      12-14-2009
On 06:46 am, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>On 12/13/2009 10:29 PM, (E-Mail Removed) wrote:
>>Doesn't matter. Sometimes it makes sense to call it directly.

>
>It only makes sense to call next (or .__next__) when you are prepared
>to explicitly catch StopIteration within a try..except construct.
>You did not catch it, so it stopped execution.
>
>Let me repeat: StopIteration is intended only for stopping iteration.
>Outside that use, it is a normal exception with no special meaning.


You cut out the part of my message where I wrote that one might have
forgotten the exception handling code that you posit is required, and
that the current behavior makes debugging this situation unnecessarily
challenging.

Jean-Paul
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-14-2009
On Mon, 14 Dec 2009 14:31:44 +0000, exarkun wrote:

> On 06:46 am, (E-Mail Removed) wrote:
>>On 12/13/2009 10:29 PM, (E-Mail Removed) wrote:
>>>Doesn't matter. Sometimes it makes sense to call it directly.

>>
>>It only makes sense to call next (or .__next__) when you are prepared to
>>explicitly catch StopIteration within a try..except construct. You did
>>not catch it, so it stopped execution.
>>
>>Let me repeat: StopIteration is intended only for stopping iteration.
>>Outside that use, it is a normal exception with no special meaning.

>
> You cut out the part of my message where I wrote that one might have
> forgotten the exception handling code that you posit is required, and
> that the current behavior makes debugging this situation unnecessarily
> challenging.


I don't see why you think this is any more challenging to debug than any
other equivalent bug. If anything I would think it was easier to debug:
if the problem is that you get a StopIteration traceback, well that's
easy and straightforward, and if the problem is that you don't (and
consequently you end up with fewer items in the list than you expect),
the obvious debugging technique is to build the list by hand and inspect
each item before adding it to the list:

L = []
for i, item in enumerate(iterable):
print i, item,
value = item() # raises StopIteration
print value
L.append(value)

That will expose the StopIteration exception and reveal the problem.

But even if I have missed something, and it is a challenging problem to
debug, oh well. It should be a quite unusual situation to come across.


--
Steven
 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      12-14-2009
On Dec 14, 2:48*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
> On Mon, 14 Dec 2009 14:31:44 +0000, exarkun wrote:
> > On 06:46 am, (E-Mail Removed) wrote:
> >>On 12/13/2009 10:29 PM, (E-Mail Removed) wrote:
> >>>Doesn't matter. Sometimes it makes sense to call it directly.

>
> >>It only makes sense to call next (or .__next__) when you are prepared to
> >>explicitly catch StopIteration within a try..except construct. You did
> >>not catch it, so it stopped execution.

>
> >>Let me repeat: StopIteration is intended only for stopping iteration.
> >>Outside that use, it is a normal exception with no special meaning.

>
> > You cut out the part of my message where I wrote that one might have
> > forgotten the exception handling code that you posit is required, and
> > that the current behavior makes debugging this situation unnecessarily
> > challenging.

>
> I don't see why you think this is any more challenging to debug than any
> other equivalent bug.


"Errors should never pass silently."

I'm not saying it's necessarily difficult to debug--although building
a list by hand to test it is a lot more work than reading an exception
traceback--but it'a stark violation of a Zen and common sense, so it
is more serious than other sorts of errors.


Carl Banks
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-15-2009
On Mon, 14 Dec 2009 15:26:25 -0800, Carl Banks wrote:

> On Dec 14, 2:48┬*pm, Steven D'Aprano <st...@REMOVE-THIS-
> cybersource.com.au> wrote:
>> On Mon, 14 Dec 2009 14:31:44 +0000, exarkun wrote:
>> > On 06:46 am, (E-Mail Removed) wrote:
>> >>On 12/13/2009 10:29 PM, (E-Mail Removed) wrote:
>> >>>Doesn't matter. Sometimes it makes sense to call it directly.

>>
>> >>It only makes sense to call next (or .__next__) when you are prepared
>> >>to explicitly catch StopIteration within a try..except construct. You
>> >>did not catch it, so it stopped execution.

>>
>> >>Let me repeat: StopIteration is intended only for stopping iteration.
>> >>Outside that use, it is a normal exception with no special meaning.

>>
>> > You cut out the part of my message where I wrote that one might have
>> > forgotten the exception handling code that you posit is required, and
>> > that the current behavior makes debugging this situation
>> > unnecessarily challenging.

>>
>> I don't see why you think this is any more challenging to debug than
>> any other equivalent bug.

>
> "Errors should never pass silently."


StopIteration isn't an error, it's a signal. The error is *misusing*
StopIteration, and the above Zen no more applies than it would if I did
x-y instead of y-x and complained that I got no traceback. Some errors
are programming mistakes, and they are the deadliest error because they
can and do pass silently. There's nothing you can do about that except
Don't Make Mistakes.


> I'm not saying it's necessarily difficult to debug--although building a
> list by hand to test it is a lot more work than reading an exception
> traceback


Of course. Nobody said the life of a programmer was all beer and
skittles


> --but it'a stark violation of a Zen and common sense, so it is
> more serious than other sorts of errors.


I'm happy to accept it is a Gotcha, but a bug? I'm not convinced.



--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Dangerous behavior of list(generator) exarkun@twistedmatrix.com Python 4 01-02-2010 08:27 PM
Re: Dangerous behavior of list(generator) Gabriel Genellina Python 14 01-02-2010 08:17 PM
Re: Dangerous behavior of list(generator) exarkun@twistedmatrix.com Python 10 12-14-2009 10:35 AM
Dangerous behavior of list(generator) Tom Machinski Python 0 12-13-2009 12:15 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments