On Dec 11, 2007 2:19 PM, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Tue, 11 Dec 2007 15:06:31 +0000, Neil Cerutti wrote: > > > When I use languages that supply do-while or do-until looping constructs > > I rarely need them. > ... > > However, did you have an specific need for a do-while construct? Perhaps > > we could show you the alternatives. > > "Need" is a strong word. After all, all looping constructs could be done > with a conditional jump, so arguably we don't "need" while or for either. > > But loops that run at least once is a basic element of algorithms. > Perhaps not as common as the zero or more times of the while loop, but > still fundamental. It is a shame it has to be faked using: >
I agree that it's fundamental, but I'd like to mention that I've written many thousands of lines of Python code, from throwaway code for demonstration to enterprisey servers and all sorts of things in between and I've *never* written a "1 or more times" loop except when I was demonstrating that exact thing. One thing that Python has definitely changed my thinking about is that I tend to formulate both problems and solutions in terms of iteration over sequence rather than as traditional conditional based looping. If I need a "1 or more" loop I formulate the problem as a sequence of 1 or more elements. This isn't something I go out of my way to do, simply the way I think about problems in Python. I just did a scan of some of the code I've been working on for the use of while. This is a few thousand LOC (and one project, an SVN renderer, doesn't have any use if it at all) and I've got exactly 3 instances. I've got a while True: as the mainloop of a thread, which technically qualifies as a "1 or more" loop but doesn't count because the exit condition is complicated and couldn't be easily written with do..until (you'd either break the same way or you'd have to set a flag instead, which is ugly). I've got the use of while as a counter in some code that unpacks bits from an integer (interop with C structures). It's a 0 or more loop. And I've got an iterator for a linked list, which is close but I don't want to yield anything if the head is None, so it's in a plain while: style: def linkedListIter(head, nextName="next"): current = head while current: yield current current = getattr(current, nextName) -- http://mail.python.org/mailman/listinfo/python-list