Re: Ned Batchelder: Loop Like A Native
On Sunday, August 7, 2016 at 2:26:48 AM UTC+1, Ned Batchelder wrote: > On Saturday, August 6, 2016 at 9:21:24 PM UTC-4, Lawrence D’Oliveiro wrote: > > On Sunday, August 7, 2016 at 11:36:06 AM UTC+12, Ned Batchelder wrote: > > > > > Didn't we already do this debate? > > > > I understand. You want to discuss loops, just not *those* sorts of loops... > > I didn't post this link here. I'm merely pointing out that your concern > about multiple ways to exit loops sounds like exactly what was discussed > here two months ago. Is there any reason to think there is new material > to discuss? > > --Ned. Thank you Ned for being far more diplomatic than I would have been. My round. When can you get over https://bearbeerfamily.co.uk/the-saxon-bear/ ? :) Kindest regards. Mark Lawrence. -- https://mail.python.org/mailman/listinfo/python-list
Re: Ned Batchelder: Loop Like A Native
On Mon, 8 Aug 2016 09:19 am, Rick Johnson wrote: > On Saturday, August 6, 2016 at 10:43:01 PM UTC-5, Steven D'Aprano wrote: > >> Yes. The two ways of ending the loop are distinct and different: >> >> - reach the end, and stop; >> - bail out early. >> >> >> When you read a book, there are two ways of stopping: >> >> - reach the end, and run out of pages to read, so you stop; >> - give up reading early, and just put the book away. >> >> (Or possibly throw the book across the room.) >> >> >> Why would you treat these two cases in the same way? >> >> interested = True >> for page in book: >> if interested: >> read(page) >> if bored_now(): >> interested = False > > This algorithm does not exit early when `interested` becomes false, > instead, it iterates every page of the book regardless of the value of > `interested`. Was this intentional, or merely a mistake? I said: "Why would you treat these two cases in the same way?", then showed a code snippet which treats the two cases in the same way (i.e. iterates over every page of the book). Of course it was intentional. > >> finished = False >> while not finished: >> try: >> page = next(book) >> except StopIteration: >> finished = True >> else: >> read(page) >> if bored_now(): >> finished = True > > That kind of code, whilst being "somewhat" idiomatic python, is horrific. Of course it is horrific. And I don't think it is even the slightest bit Pythonic. There may (very unlikely, but theoretically possible) be occasions where you have no choice but to write code like this, but if so you wouldn't describe it as idiomatic Python code. > If the intent is to iterate the pages of a book until the end is reached > *OR* until the "reader's interest wanes", then a simple for-loop will > suffice. That's exactly the sort of thing that Lawrence is objecting to, because (and I quote the part of my email you deleted): "A loop like [example with break] actually has two different ways to terminate. Is there any good reason for them to be written two different ways?" > for page in book: > isReaderInterested = reader.read_page(page) > if not isReaderInterested: > break > > Simple is better than complex. Precisely. I'm showing the horrible code you have to write to avoid writing two different ways of terminating a loop with two fundamentally different ways of terminating. Lawrence, if you're still reading, I hope I am not misrepresenting your position here. If you think that there is an alternative way of writing this code which avoids two exits from the loop but without having to write such unidiomatic (if not idiotic) code, please tell us what you would do. There is one clean way for doing this: use itertools. def interesting(page): """Return True if the page is interesting, False if boring.""" for page in itertools.takewhile(interesting, book): reader.read_page(page) but it's not always easy to re-write the code to match that idiom. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: Ned Batchelder: Loop Like A Native
On Saturday, August 6, 2016 at 10:43:01 PM UTC-5, Steven D'Aprano wrote: > Yes. The two ways of ending the loop are distinct and different: > > - reach the end, and stop; > - bail out early. > > > When you read a book, there are two ways of stopping: > > - reach the end, and run out of pages to read, so you stop; > - give up reading early, and just put the book away. > > (Or possibly throw the book across the room.) > > > Why would you treat these two cases in the same way? > > interested = True > for page in book: > if interested: > read(page) > if bored_now(): > interested = False This algorithm does not exit early when `interested` becomes false, instead, it iterates every page of the book regardless of the value of `interested`. Was this intentional, or merely a mistake? > finished = False > while not finished: > try: > page = next(book) > except StopIteration: > finished = True > else: > read(page) > if bored_now(): > finished = True That kind of code, whilst being "somewhat" idiomatic python, is horrific. If the intent is to iterate the pages of a book until the end is reached *OR* until the "reader's interest wanes", then a simple for-loop will suffice. for page in book: isReaderInterested = reader.read_page(page) if not isReaderInterested: break Simple is better than complex. -- https://mail.python.org/mailman/listinfo/python-list
Re: Ned Batchelder: Loop Like A Native
On Sunday, August 7, 2016 at 6:52:45 PM UTC-4, Lawrence D’Oliveiro wrote: > On Sunday, August 7, 2016 at 1:26:48 PM UTC+12, Ned Batchelder wrote: > > I'm merely pointing out that your concern about multiple ways to exit loops > > sounds like exactly what was discussed here two months ago. > > And one could point out that your presentation on Python loops sounds exactly > like every other discussion on Python loops. If you feel like my presentation was worthless, that would certainly be something we could discuss here, respectfully. I hope you don't actually feel that way. If Mark had posted this link in June, and then again now, I think it would be reasonable for someone to say, "This was just posted in June, why repost it now?" > > > Is there any reason to think there is new material to discuss? > > There are always new things to learn, even about such basic things as loops. > Though some don’t seem to like it when I point this out... We had a long thread about your point only two months ago. Is there something new to add to it? I thought you made your point then, and had a long discussion about it. You'll have to decide if you think there are new points to be made. This list has had difficulty in the past with people who try to start the same topics over and over again, because people didn't agree with them the last three (or ten or twenty) times they brought it up. It's unpleasant and noisy, and pollutes the tone of other conversations. I don't mean to put you into that category. If you have new ideas about loop termination that didn't get an airing in June, I'm sure people will be interested to discuss them. --Ned. -- https://mail.python.org/mailman/listinfo/python-list
Re: Ned Batchelder: Loop Like A Native
On Sunday, August 7, 2016 at 1:26:48 PM UTC+12, Ned Batchelder wrote: > I'm merely pointing out that your concern about multiple ways to exit loops > sounds like exactly what was discussed here two months ago. And one could point out that your presentation on Python loops sounds exactly like every other discussion on Python loops. > Is there any reason to think there is new material to discuss? There are always new things to learn, even about such basic things as loops. Though some don’t seem to like it when I point this out... -- https://mail.python.org/mailman/listinfo/python-list
Re: Ned Batchelder: Loop Like A Native
On Sun, Aug 7, 2016 at 1:42 PM, Steven D'Apranowrote: > When you read a book, there are two ways of stopping: > > - reach the end, and run out of pages to read, so you stop; > - give up reading early, and just put the book away. > > (Or possibly throw the book across the room.) > Or someone might rip the book out of your hands (unexpected exception). You don't need to handle that case specifically, but it will terminate the loop. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Ned Batchelder: Loop Like A Native
On Sun, 7 Aug 2016 08:05 am, Lawrence D’Oliveiro wrote: > On Saturday, August 6, 2016 at 12:08:30 PM UTC+12, bream...@gmail.com > wrote: >> A couple or three years old but this is well worth seeing for anybody, >> regardless of your Python expertise. >> http://nedbatchelder.com/text/iter.html > > A loop like > > for i in ... : >... >if ... cond ... : >break >... > #end for > > actually has two different ways to terminate. Is there any good reason for > them to be written two different ways? Yes. The two ways of ending the loop are distinct and different: - reach the end, and stop; - bail out early. When you read a book, there are two ways of stopping: - reach the end, and run out of pages to read, so you stop; - give up reading early, and just put the book away. (Or possibly throw the book across the room.) Why would you treat these two cases in the same way? interested = True for page in book: if interested: read(page) if bored_now(): interested = False finished = False while not finished: try: page = next(book) except StopIteration: finished = True else: read(page) if bored_now(): finished = True -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: Ned Batchelder: Loop Like A Native
On Saturday, August 6, 2016 at 9:21:24 PM UTC-4, Lawrence D’Oliveiro wrote: > On Sunday, August 7, 2016 at 11:36:06 AM UTC+12, Ned Batchelder wrote: > > > Didn't we already do this debate? > > I understand. You want to discuss loops, just not *those* sorts of loops... I didn't post this link here. I'm merely pointing out that your concern about multiple ways to exit loops sounds like exactly what was discussed here two months ago. Is there any reason to think there is new material to discuss? --Ned. -- https://mail.python.org/mailman/listinfo/python-list
Re: Ned Batchelder: Loop Like A Native
On Sunday, August 7, 2016 at 11:36:06 AM UTC+12, Ned Batchelder wrote: > Didn't we already do this debate? I understand. You want to discuss loops, just not *those* sorts of loops... -- https://mail.python.org/mailman/listinfo/python-list
Re: Ned Batchelder: Loop Like A Native
On Saturday, August 6, 2016 at 7:36:06 PM UTC-4, Ned Batchelder wrote: > On Saturday, August 6, 2016 at 6:06:27 PM UTC-4, Lawrence D’Oliveiro wrote: > > On Saturday, August 6, 2016 at 12:08:30 PM UTC+12, bream...@gmail.com wrote: > > > A couple or three years old but this is well worth seeing for anybody, > > > regardless of your Python expertise. > > > http://nedbatchelder.com/text/iter.html > > > > A loop like > > > > for i in ... : > >... > >if ... cond ... : > >break > >... > > #end for > > > > actually has two different ways to terminate. Is there any good reason for > > them to be written two different ways? > > Didn't we already do this debate? For example: https://mail.python.org/pipermail/python-list/2016-June/709758.html Maybe we don't have to revisit it... :) --Ned. -- https://mail.python.org/mailman/listinfo/python-list
Re: Ned Batchelder: Loop Like A Native
On Saturday, August 6, 2016 at 6:06:27 PM UTC-4, Lawrence D’Oliveiro wrote: > On Saturday, August 6, 2016 at 12:08:30 PM UTC+12, bream...@gmail.com wrote: > > A couple or three years old but this is well worth seeing for anybody, > > regardless of your Python expertise. http://nedbatchelder.com/text/iter.html > > A loop like > > for i in ... : >... >if ... cond ... : >break >... > #end for > > actually has two different ways to terminate. Is there any good reason for > them to be written two different ways? Didn't we already do this debate? --Ned. -- https://mail.python.org/mailman/listinfo/python-list
Re: Ned Batchelder: Loop Like A Native
On Saturday, August 6, 2016 at 12:08:30 PM UTC+12, bream...@gmail.com wrote: > A couple or three years old but this is well worth seeing for anybody, > regardless of your Python expertise. http://nedbatchelder.com/text/iter.html A loop like for i in ... : ... if ... cond ... : break ... #end for actually has two different ways to terminate. Is there any good reason for them to be written two different ways? -- https://mail.python.org/mailman/listinfo/python-list
Ned Batchelder: Loop Like A Native
A couple or three years old but this is well worth seeing for anybody, regardless of your Python expertise. http://nedbatchelder.com/text/iter.html Kindest regards. Mark Lawrence. -- https://mail.python.org/mailman/listinfo/python-list