Re: The rap against "while True:" loops

2009-10-21 Thread Jorgen Grahn
On Wed, 2009-10-14, Steven D'Aprano wrote: ... > Setting up a try...except block is cheap in Python. According to my > tests, the overhead is little more than that of a single pass statement. > > But actually raising and catching the exception is not cheap. If you use > a lot of exceptions for f

Re: The rap against "while True:" loops

2009-10-20 Thread Lawrence D'Oliveiro
In message , kj wrote: > I use "while True"-loops often, and intend to continue doing this > "while True" ... My practice with regard to loops is to use constructs such as "while (condition) { ... }" and "do ... while (condition)" where "condition" is the ONLY terminating condition (i.e. no "br

Re: The rap against "while True:" loops

2009-10-20 Thread Tim Rowe
2009/10/20 Hendrik van Rooyen : > So if you want to sum stuff where there are a lot of keys, but only a few > values per key - say between one and ten, then it would be faster to look > before you leap. > > On the other hand, if there are relatively few keys and tens or hundreds of > values per ke

Re: The rap against "while True:" loops

2009-10-20 Thread NiklasRTZ
On Oct 19, 2:51 am, Hendrik van Rooyen wrote: > On Sunday, 18 October 2009 11:31:19 Paul Rubin wrote: > > > Hendrik van Rooyen writes: > > > Standard Python idiom: > > > > if key in d: > > >   d[key] += value > > > else: > > >   d[key] = value > > > The issue is that uses two lookups.  If that's

Re: The rap against "while True:" loops

2009-10-20 Thread Iain King
On Oct 19, 7:51 am, Hendrik van Rooyen wrote: > On Sunday, 18 October 2009 11:31:19 Paul Rubin wrote: > > > Hendrik van Rooyen writes: > > > Standard Python idiom: > > > > if key in d: > > >   d[key] += value > > > else: > > >   d[key] = value > > > The issue is that uses two lookups.  If that's

Re: The rap against "while True:" loops

2009-10-20 Thread Hendrik van Rooyen
On Monday, 19 October 2009 09:43:15 Steven D'Aprano wrote: > On Mon, 19 Oct 2009 08:51:44 +0200, Hendrik van Rooyen wrote: > > The point I was trying to make > > subliminally, was that there is a relative cost of double lookup for all > > cases versus exceptions for some cases. - Depending on the f

Re: The rap against "while True:" loops

2009-10-19 Thread Jaime Buelta
For me, it's more a question of clarity than anything else. I don't like very much using break, continue or more than one return per function on C/C++, but sometimes it's much clearer to use them. Also, in Python I use them often, as usually the code is cleaner this way. for example, I will wrote

Re: The rap against "while True:" loops

2009-10-19 Thread inhahe
On Mon, Oct 19, 2009 at 1:55 PM, inhahe wrote: > > > On Sat, Oct 10, 2009 at 6:34 PM, Mensanator wrote: > >> On Oct 10, 5:02�pm, kj wrote: >> > In <01ccc46d-5ea9-4dfe-ba22-699c6b859...@v36g2000yqv.googlegroups.com> >> Mensanator writes: >> > >> > In fact, if it were up to me, I would have made

Re: The rap against "while True:" loops

2009-10-19 Thread Tim Rowe
2009/10/18 Steven D'Aprano : > That confuses me. If I call: > > y = mydict[x] > > how does my knowledge of what to do if x is not a key relate to whether > the language raises an exception, returns an error code, dumps core, or > prints "He's not the Messiah, he's a very naughty boy" to stderr? >

Re: The rap against "while True:" loops

2009-10-19 Thread Steven D'Aprano
On Mon, 19 Oct 2009 08:51:44 +0200, Hendrik van Rooyen wrote: > The point I was trying to make > subliminally, was that there is a relative cost of double lookup for all > cases versus exceptions for some cases. - Depending on the frequency of > "some", I would expect a breakeven point. There is,

Re: The rap against "while True:" loops

2009-10-18 Thread Hendrik van Rooyen
On Sunday, 18 October 2009 11:31:19 Paul Rubin wrote: > Hendrik van Rooyen writes: > > Standard Python idiom: > > > > if key in d: > > d[key] += value > > else: > > d[key] = value > > The issue is that uses two lookups. If that's ok, the more usual idiom is: > > d[key] = value + d.get(key,

Re: The rap against "while True:" loops

2009-10-18 Thread NiklasRTZ
Russ P. wrote: > On Oct 10, 1:15pm, kj wrote: > > I'm coaching a group of biologists on basic Python scripting. One > > of my charges mentioned that he had come across the advice never > > to use loops beginning with "while True". Of course, that's one > > way to start an infinite loop, but thi

Re: The rap against "while True:" loops

2009-10-18 Thread Paul Rubin
Lie Ryan writes: > If key and value had been anything but simple variable/name, then > definitely you're writing the try-block the wrong way. try-block must > be kept as simple as possible. Avoiding the try-block completely is the simplest possibility of them all. -- http://mail.python.org/mailm

Re: The rap against "while True:" loops

2009-10-18 Thread Lie Ryan
Ben Finney wrote: Lie Ryan writes: Paul Rubin wrote: Steven D'Aprano writes: For the record, the four lines Paul implies are "confusing" are: try: d[key] += value except KeyError: d[key] = value Consider what happens if the computation of "key" or "value" itself raises KeyError.

Re: The rap against "while True:" loops

2009-10-18 Thread Steven D'Aprano
On Sat, 17 Oct 2009 13:12:52 +0100, Tim Rowe wrote: > 2009/10/17 Steven D'Aprano : > >> No, you have a fundamental misunderstanding. They're called exceptions, >> not errors, because they represent exceptional cases. Often errors are >> exceptional cases, but they're not the only sort of exceptio

Re: The rap against "while True:" loops

2009-10-18 Thread Ben Finney
Lie Ryan writes: > Paul Rubin wrote: > > Steven D'Aprano writes: > >> For the record, the four lines Paul implies are "confusing" are: > >> > >> try: > >> d[key] += value > >> except KeyError: > >> d[key] = value > > > > Consider what happens if the computation of "key" or "value" itself

Re: The rap against "while True:" loops

2009-10-18 Thread Paul Rubin
Hendrik van Rooyen writes: > Standard Python idiom: > > if key in d: > d[key] += value > else: > d[key] = value The issue is that uses two lookups. If that's ok, the more usual idiom is: d[key] = value + d.get(key, 0) -- http://mail.python.org/mailman/listinfo/python-list

Re: The rap against "while True:" loops

2009-10-18 Thread Lie Ryan
Paul Rubin wrote: Steven D'Aprano writes: For the record, the four lines Paul implies are "confusing" are: try: d[key] += value except KeyError: d[key] = value Consider what happens if the computation of "key" or "value" itself raises KeyError. Isn't key and value just a simple var

Re: The rap against "while True:" loops

2009-10-18 Thread Steven D'Aprano
On Sat, 17 Oct 2009 23:37:51 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> For the record, the four lines Paul implies are "confusing" are: >> >> try: >> d[key] += value >> except KeyError: >> d[key] = value > > Consider what happens if the computation of "key" or "value" itself

Re: The rap against "while True:" loops

2009-10-18 Thread Hendrik van Rooyen
On Saturday, 17 October 2009 16:30:55 Aahz wrote: > In article , > > Tim Rowe wrote: > >The point is that an exception causes a change in program flow, so of > >course they're used for flow control. It's what they do. The question > >is in what cases it's appropriate to use them. > > Standard Pyt

Re: The rap against "while True:" loops

2009-10-17 Thread Paul Rubin
Steven D'Aprano writes: > For the record, the four lines Paul implies are "confusing" are: > > try: > d[key] += value > except KeyError: > d[key] = value Consider what happens if the computation of "key" or "value" itself raises KeyError. -- http://mail.python.org/mailman/listinfo/pytho

Re: The rap against "while True:" loops

2009-10-17 Thread Steven D'Aprano
On Sat, 17 Oct 2009 15:22:59 -0700, Paul Rubin wrote: > Terry Reedy writes: >> >d[key] += value >> >> Yes, the motivation was to reduce 4 lines to 1 line for a common use >> case, and not because of any sense of 'inappropriateness'. > > Reducing 4 confusing lines to 1 clear one is almost al

Re: The rap against "while True:" loops

2009-10-17 Thread Paul Rubin
Terry Reedy writes: > >d[key] += value > > Yes, the motivation was to reduce 4 lines to 1 line for a common use > case, and not because of any sense of 'inappropriateness'. Reducing 4 confusing lines to 1 clear one is almost always appropriate. -- http://mail.python.org/mailman/listinfo/pyt

Re: The rap against "while True:" loops

2009-10-17 Thread Terry Reedy
Steven D'Aprano wrote: On Fri, 16 Oct 2009 18:30:50 +0100, Tim Rowe wrote: Also, using exceptions this way is a structured form of GOTO -- it's easy to abuse and turn it into spaghetti code. Actually, not that easy to abuse, because you can't jump back into the try block. It's more like a multi

Re: The rap against "while True:" loops

2009-10-17 Thread Terry Reedy
Paul Rubin wrote: a...@pythoncraft.com (Aahz) writes: Standard Python idiom: try: d[key] += value except KeyError: d[key] = value Maybe you need to re-think "appropriate". But more recent style prefers: d = collections.defaultdict(int) ... d[key] += value Yes, the motivat

Re: The rap against "while True:" loops

2009-10-17 Thread Paul Rubin
a...@pythoncraft.com (Aahz) writes: > > d = collections.defaultdict(int) > > ... > > d[key] += value > > That was a trivial example; non-trivial examples not addressed by > defaultdict are left as an exercise for the reader. Even in the "nontrivial" examples, I think avoiding the exception

Re: The rap against "while True:" loops

2009-10-17 Thread Aahz
In article <7xmy3peq3s@ruckus.brouhaha.com>, Paul Rubin wrote: >a...@pythoncraft.com (Aahz) writes: >> >> Standard Python idiom: >> >> try: >> d[key] += value >> except KeyError: >> d[key] = value >> >> Maybe you need to re-think "appropriate". > >But m

Re: The rap against "while True:" loops

2009-10-17 Thread Paul Rubin
a...@pythoncraft.com (Aahz) writes: > Standard Python idiom: > > try: > d[key] += value > except KeyError: > d[key] = value > > Maybe you need to re-think "appropriate". But more recent style prefers: d = collections.defaultdict(int) ... d[key] += value -- http://mail.python.o

Re: The rap against "while True:" loops

2009-10-17 Thread Aahz
In article , Tim Rowe wrote: > >The point is that an exception causes a change in program flow, so of >course they're used for flow control. It's what they do. The question >is in what cases it's appropriate to use them. Standard Python idiom: try: d[key] += value except KeyError: d[key

Re: The rap against "while True:" loops

2009-10-17 Thread Tim Rowe
2009/10/17 Steven D'Aprano : > No, you have a fundamental misunderstanding. They're called exceptions, > not errors, because they represent exceptional cases. Often errors are > exceptional cases, but they're not the only sort of exceptional case. The whole reason for the mechanism, across all la

Re: The rap against "while True:" loops

2009-10-17 Thread Steven D'Aprano
On Fri, 16 Oct 2009 18:30:50 +0100, Tim Rowe wrote: >> Also, using exceptions this way is a structured form of GOTO -- it's >> easy to abuse and turn it into spaghetti code. Actually, not that easy >> to abuse, because you can't jump back into the try block. It's more >> like a multi-level break o

Re: The rap against "while True:" loops

2009-10-16 Thread Russ P.
On Oct 10, 1:15 pm, kj wrote: > I'm coaching a group of biologists on basic Python scripting.  One > of my charges mentioned that he had come across the advice never > to use loops beginning with "while True".  Of course, that's one > way to start an infinite loop, but this seems hardly a sufficie

Re: The rap against "while True:" loops

2009-10-16 Thread Bearophile
Paul Rubin: >  http://scholar.google.com/scholar?cluster=17368311454828547380 > > Keep in mind that the article is 35 years old though, and is purely > imperative.  Lots of stuff done with cockamamie looping constructs is > more cleanly done with Python generators, itertools, higher-order > functi

Re: The rap against "while True:" loops

2009-10-16 Thread Aahz
In article , Tim Rowe wrote: > >The understood meaning of throwing an exception is to say "something >happened that shouldn't have". If one uses it when something has >happened that *should* have, because it happens to have the right >behaviour (even if the overhead doesn't matter), then one is >

Re: The rap against "while True:" loops

2009-10-16 Thread Tim Rowe
2009/10/15 Steven D'Aprano : > Setting up a try...except block is cheap in Python. According to my > tests, the overhead is little more than that of a single pass statement. > > But actually raising and catching the exception is not cheap. If you use > a lot of exceptions for flow control, perform

Re: The rap against "while True:" loops

2009-10-16 Thread Tim Rowe
2009/10/15 Steven D'Aprano : >> And with enough static analysis to guarantee that the break will be >> reached? I think it would be a bit much to expect Python to solve the >> halting problem! Not to what I thought was being proposed -- it seemed to make the break mandatory, not merely the only c

Re: The rap against "while True:" loops

2009-10-15 Thread Tim Rowe
2009/10/11 Philip Semanchuk : > IMHO, break, goto, etc. have their place, but they're ripe for abuse which > leads to spaghetti code. Unrestricted goto can leat to spaghetti code, but surely break can't? AFAICS, any break construct will still be H-K reducible. -- Tim Rowe -- http://mail.python.

Re: The rap against "while True:" loops

2009-10-14 Thread Mensanator
On Oct 14, 12:07�pm, Ethan Furman wrote: > Mensanator wrote: > > On Oct 14, 2:19 am, Dennis Lee Bieber wrote: > > >>On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator > >> declaimed the following in > >>gmane.comp.python.general: > > >>>You're not getting away that easy. > > >>>What's YOUR opin

Re: The rap against "while True:" loops

2009-10-14 Thread Mensanator
On Oct 14, 6:08�pm, Steven D'Aprano wrote: > On Wed, 14 Oct 2009 09:34:28 -0700, Mensanator wrote: > > On Oct 14, 2:19 am, Dennis Lee Bieber wrote: > >> On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator > >> declaimed the following in > >> gmane.comp.python.general: > > >> > You're not gettin

Re: The rap against "while True:" loops

2009-10-14 Thread Paul Rubin
Steven D'Aprano writes: > Why should Python make that guarantee about this hypothetical "loop > forever" construct? It doesn't make much sense for Python as normally practiced. Termination proofs (aka proofs of progress) are used in formal verification systems to make sure that the verification

Re: The rap against "while True:" loops

2009-10-14 Thread Steven D'Aprano
On Wed, 14 Oct 2009 20:17:40 +, Jorgen Grahn wrote: >> But we have exceptions. And I know somebody, in other languages, thinks >> it's a Best Practice to avoid using exceptions for flow control. > > A lot of C++ programmers think so, and Stroustrup himself says > "exceptions are for exception

Re: The rap against "while True:" loops

2009-10-14 Thread Steven D'Aprano
On Wed, 14 Oct 2009 18:30:06 +0100, Tim Rowe wrote: > 2009/10/14 Dennis Lee Bieber : > >>        If anything -- I'd suggest a proposal to add a plain    loop >>           as a >> keyword in Python, whose effect is equivalent to a "while True", but a >> break    must be used to exit said loop (wel

Re: The rap against "while True:" loops

2009-10-14 Thread Steven D'Aprano
On Wed, 14 Oct 2009 09:34:28 -0700, Mensanator wrote: > On Oct 14, 2:19�am, Dennis Lee Bieber wrote: >> On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator >> declaimed the following in >> gmane.comp.python.general: >> >> > You're not getting away that easy. >> >> > What's YOUR opinion of "whil

Re: The rap against "while True:" loops

2009-10-14 Thread Rhodri James
On Wed, 14 Oct 2009 02:26:17 +0100, Mensanator wrote: On Oct 13, 5:38�pm, "Rhodri James" wrote: On Tue, 13 Oct 2009 22:59:04 +0100, Mensanator wrote: > And I'm not saying John nor the OP should stop > using what works for them. But there are certainly > valid reasons for "don't use while T

Re: The rap against "while True:" loops

2009-10-14 Thread Tim Rowe
2009/10/12 RDrewD : > I was a bit surprised that nobody in this discussion so far bantered > around the phrase "loop invariant", but then I looked in > http://en.wikipedia.org/wiki/Loop_invariant and found it was draped in > so much formalism that it's sure to put off all but the most dedicated > o

Re: The rap against "while True:" loops

2009-10-14 Thread Jack Norton
kj wrote: I'm coaching a group of biologists on basic Python scripting. One of my charges mentioned that he had come across the advice never to use loops beginning with "while True". Of course, that's one way to start an infinite loop, but this seems hardly a sufficient reason to avoid the con

Re: The rap against "while True:" loops

2009-10-14 Thread Paul Rubin
Raymond Hettinger writes: > IIRC, the C++ admonition against using exceptions for flow control > was rooted in performance concerns specific to that language and > its compilers. It was not stylistic advice and did not deny that > flow control exceptions could provide elegant solutions to some >

Re: The rap against "while True:" loops

2009-10-14 Thread Raymond Hettinger
> And I know somebody, in other languages, thinks > it's a Best Practice to avoid using exceptions for flow control. Ah, now we have two code prions in just one thread. I hope no newbie or supervisor reads this thread and latches on to those two counter-productive ideas. ISTM, both ideas are dang

Re: The rap against "while True:" loops

2009-10-14 Thread Jorgen Grahn
On Wed, 2009-10-14, Marco Mariani wrote: > Dennis Lee Bieber wrote: > >> One thing to note is that "break" ONLY exits the innermost loop -- >> Ada adds the confusion that one could define a label on the loops, and >> have the innermost use >> exit outer_label [when condition] >> >> >>

Re: The rap against "while True:" loops

2009-10-14 Thread Jorgen Grahn
On Mon, 2009-10-12, Grant Edwards wrote: > On 2009-10-12, Gabriel Genellina wrote: > >>> my_prissy_little_indicator_variable = true >>> while (my_prissy_little_indicator_variable){ >>> >>> } >>> isn't satisfying because it doesn't guard the with any >>> assurance that the loop invariant will

Re: The rap against "while True:" loops

2009-10-14 Thread Jorgen Grahn
On Mon, 2009-10-12, RDrewD wrote: ... > I was a bit surprised that nobody in this discussion so far bantered > around the phrase "loop invariant", but then I looked in > http://en.wikipedia.org/wiki/Loop_invariant and found it was draped in > so much formalism that it's sure to put off all but the

Re: The rap against "while True:" loops

2009-10-14 Thread Ethan Furman
Mensanator wrote: On Oct 14, 2:19�am, Dennis Lee Bieber wrote: On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator declaimed the following in gmane.comp.python.general: You're not getting away that easy. What's YOUR opinion of "whilr True"? � � � � Uhm... that it isn't valid in any l

Re: The rap against "while True:" loops

2009-10-14 Thread Tim Rowe
2009/10/14 Dennis Lee Bieber : >        If anything -- I'd suggest a proposal to add a plain    loop    as a > keyword in Python, whose effect is equivalent to a "while True", but a > break    must be used to exit said loop (well, we'll ignore raising an > exception ) And with enough static analy

Re: The rap against "while True:" loops

2009-10-14 Thread MRAB
Dennis Lee Bieber wrote: On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator declaimed the following in gmane.comp.python.general: You're not getting away that easy. What's YOUR opinion of "whilr True"? Uhm... that it isn't valid in any language having English influence upon it's k

Re: The rap against "while True:" loops

2009-10-14 Thread Mensanator
On Oct 14, 2:19�am, Dennis Lee Bieber wrote: > On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator > declaimed the following in > gmane.comp.python.general: > > > You're not getting away that easy. > > > What's YOUR opinion of "whilr True"? > > � � � � Uhm... that it isn't valid in any language

Re: The rap against "while True:" loops

2009-10-14 Thread TerryP
When all is said and done, is not all looping *basically* equivalent to something like this? begin_loop: unless TEST goto end_loop ;; loop body here if TEST goto begin_loop end_loop: Which could likely be used to implement something like: while TEST: # loop body

Re: The rap against "while True:" loops

2009-10-14 Thread Tim Rowe
2009/10/14 Marco Mariani : > Dennis Lee Bieber wrote: > >>        One thing to note is that "break" ONLY exits the innermost loop -- >> Ada adds the confusion that one could define a label on the loops, and >> have the innermost use >>        exit outer_label [when condition] >> >> >>        THAT I

Re: The rap against "while True:" loops

2009-10-14 Thread Marco Mariani
Dennis Lee Bieber wrote: One thing to note is that "break" ONLY exits the innermost loop -- Ada adds the confusion that one could define a label on the loops, and have the innermost use exit outer_label [when condition] THAT I find scary... Since you have to match the l

Re: The rap against "while True:" loops

2009-10-14 Thread TerryP
Mensanator, thank goodness that was generated :-P -- http://mail.python.org/mailman/listinfo/python-list

Re: The rap against "while True:" loops

2009-10-13 Thread Mensanator
On Oct 13, 5:38�pm, "Rhodri James" wrote: > On Tue, 13 Oct 2009 22:59:04 +0100, Mensanator wrote: > > And I'm not saying John nor the OP should stop > > using what works for them. But there are certainly > > valid reasons for "don't use while True" to be > > on the "Best Practices" list. > > Unfo

Re: The rap against "while True:" loops

2009-10-13 Thread greg
Steven D'Aprano wrote: The best I have seen is that loops should have a single entry point and a single exit point, to make it easier to reason about pre- and post-conditions. But frankly I'm not convinced that's true -- or at least, multiple exists shouldn't *necessarily* leader to difficulty

Re: The rap against "while True:" loops

2009-10-13 Thread Paul Rubin
Steven D'Aprano writes: > But the consequence of that simplicity and speed is that they're not as > general as a for-loop. This was a design decision. But change the design > and you could have something like this: > > [expr for name in seq until cond] > > which breaks when cond becomes true.

Re: The rap against "while True:" loops

2009-10-13 Thread Rhodri James
On Tue, 13 Oct 2009 22:59:04 +0100, Mensanator wrote: And I'm not saying John nor the OP should stop using what works for them. But there are certainly valid reasons for "don't use while True" to be on the "Best Practices" list. Unfortunately, some of them seem to be reasons from my point of

Re: The rap against "while True:" loops

2009-10-13 Thread Steven D'Aprano
On Tue, 13 Oct 2009 14:59:04 -0700, Mensanator wrote: > And I'm not saying John nor the OP should stop using what works for > them. But there are certainly valid reasons for "don't use while True" > to be on the "Best Practices" list. "Valid"? Well, maybe. But none of them are convincing to me. T

Re: The rap against "while True:" loops

2009-10-13 Thread Paul Rubin
Mensanator writes: > And I'm not saying John nor the OP should stop using what works for > them. But there are certainly valid reasons for "don't use while > True" to be on the "Best Practices" list. > > After all, how many times hve you put 'break' in a loop > comprehension?

Re: The rap against "while True:" loops

2009-10-13 Thread Mensanator
On Oct 12, 4:59�pm, David C Ullrich wrote: > kj wrote: > > I'm coaching a group of biologists on basic Python scripting. �One > > of my charges mentioned that he had come across the advice never > > to use loops beginning with "while True". �Of course, that's one > > way to start an infinite loop,

Re: The rap against "while True:" loops

2009-10-13 Thread Mensanator
On Oct 13, 11:27�am, Ethan Furman wrote: > Mensanator wrote: > > On Oct 13, 3:44 am, John Reid wrote: > > >>while not done: > > >>seems very dangerous to me as you'd have to > > >>del done > > >>before writing the same construct again. That's the sort of thing that > >>leads to errors. > > >Duh.

Re: The rap against "while True:" loops

2009-10-13 Thread Ethan Furman
Mensanator wrote: On Oct 13, 3:44�am, John Reid wrote: while not done: seems very dangerous to me as you'd have to del done before writing the same construct again. That's the sort of thing that leads to errors. Duh. I won't write silly code like that either. If I need more than one loop

Re: The rap against "while True:" loops

2009-10-13 Thread John Reid
Mensanator wrote: No, it's just that the OP was asking whether avoiding "while True" is considered Best Practice. How can you answer such a question without sounding dogmatic? I was just pointing out your style of programming seems inflexible. "Just another line that has to be interpreted l

Re: The rap against "while True:" loops

2009-10-13 Thread Mensanator
On Oct 13, 3:44�am, John Reid wrote: > Mensanator wrote: > >> Nothing wrong with a having a break IMHO. > > > My opinion is that there is everything wrong with > > having a break. I don't think I have ever used one, > > I write code that doesn't depend on that crutch. > > I guess its crutch-iness

Re: The rap against "while True:" loops

2009-10-13 Thread Grant Edwards
On 2009-10-12, Gabriel Genellina wrote: >> my_prissy_little_indicator_variable = true >> while (my_prissy_little_indicator_variable){ >> >> } >> isn't satisfying because it doesn't guard the with any >> assurance that the loop invariant will be true before you enter into >> that block of co

Re: The rap against "while True:" loops

2009-10-13 Thread John Reid
Mensanator wrote: Nothing wrong with a having a break IMHO. My opinion is that there is everything wrong with having a break. I don't think I have ever used one, I write code that doesn't depend on that crutch. I guess its crutch-iness is in the eye of the beholder. You seem to have a dogmat

Re: The rap against "while True:" loops

2009-10-12 Thread Steven D'Aprano
On Mon, 12 Oct 2009 11:32:27 -0700, Mensanator wrote: >> Nothing wrong with a having a break IMHO. > > My opinion is that there is everything wrong with having a break. I > don't think I have ever used one, I write code that doesn't depend on > that crutch. Using break can avoid a lot of compli

Re: The rap against "while True:" loops

2009-10-12 Thread Paul Rubin
kj writes: > I use "while True"-loops often, and intend to continue doing this > "while True", but I'm curious to know: how widespread is the > injunction against such loops? Has it reached the status of "best > practice"? E. W. Dijkstra used to advocate that every loop have exactly one entry po

Re: The rap against "while True:" loops

2009-10-12 Thread Raymond Hettinger
[kj] > I use "while True"-loops often, and intend to continue doing this > "while True", but I'm curious to know: how widespread is the > injunction against such loops?  Has it reached the status of "best > practice"? This is the first I've ever heard of such an quasi-injunction. Like you, I use "

Re: The rap against "while True:" loops

2009-10-12 Thread Brian Blais
On Oct 12, 2009, at 15:18 , Falcolas wrote: Glad to hear, by the way, that you don't use gotos in Python. =D actually, it is there. http://entrian.com/goto/ I particularly like the comefrom construct! bb -- Brian Blais bbl...@bryant.edu http://web.bryant.edu/~bblais --

Re: The rap against "while True:" loops

2009-10-12 Thread David C Ullrich
kj wrote: I'm coaching a group of biologists on basic Python scripting. One of my charges mentioned that he had come across the advice never to use loops beginning with "while True". Of course, that's one way to start an infinite loop, Heh-heh: When I read this it occurred to me that another

Re: The rap against "while True:" loops

2009-10-12 Thread Mensanator
On Oct 12, 2:18 pm, Falcolas wrote: > On Oct 12, 12:32 pm, Mensanator wrote: > > > > > > > On Oct 12, 1:02 pm, John Reid wrote: > > > Mensanator wrote: > > > > On Oct 12, 3:36 am, greg wrote: > > > >> Mensanator wrote: > > > >>> while not done: > > > >>> ... > > > >>> if n==1: done = True > > >

Re: The rap against "while True:" loops

2009-10-12 Thread Falcolas
On Oct 12, 12:32 pm, Mensanator wrote: > On Oct 12, 1:02 pm, John Reid wrote: > > Mensanator wrote: > > > On Oct 12, 3:36 am, greg wrote: > > >> Mensanator wrote: > > >>> while not done: > > >>> ... > > >>> if n==1: done = True > > >>> ... > > >> Seems to me that 'while not done:' is no better t

Re: The rap against "while True:" loops

2009-10-12 Thread Mensanator
On Oct 12, 1:02�pm, John Reid wrote: > Mensanator wrote: > > On Oct 12, 3:36 am, greg wrote: > >> Mensanator wrote: > >>> while not done: > >>> ... > >>> if n==1: done = True > >>> ... > >> Seems to me that 'while not done:' is no better than > >> 'while True:', because in both cases you have to

Re: The rap against "while True:" loops

2009-10-12 Thread Ethan Furman
Mensanator wrote: On Oct 12, 3:36�am, greg wrote: Mensanator wrote: while not done: � � ... � � if n==1: done = True � � ... Seems to me that 'while not done:' is no better than 'while True:', because in both cases you have to look inside the loop to find out what the exit condition is. U

Re: The rap against "while True:" loops

2009-10-12 Thread John Reid
Mensanator wrote: On Oct 12, 3:36�am, greg wrote: Mensanator wrote: while not done: � � ... � � if n==1: done = True � � ... Seems to me that 'while not done:' is no better than 'while True:', because in both cases you have to look inside the loop to find out what the exit condition is. Usin

Re: The rap against "while True:" loops

2009-10-12 Thread Mensanator
On Oct 12, 3:36�am, greg wrote: > Mensanator wrote: > > while not done: > > � � ... > > � � if n==1: done = True > > � � ... > > Seems to me that 'while not done:' is no better than > 'while True:', because in both cases you have to look > inside the loop to find out what the exit condition > is.

Re: The rap against "while True:" loops

2009-10-12 Thread Luis Zarrabeitia
On Monday 12 October 2009 09:47:23 am Xavier Ho wrote: > On Mon, Oct 12, 2009 at 11:32 PM, Luis Zarrabeitia wrote: > > Actually, in python, this works even better: > > > > for lin in iter(file_object.readline, ""): > >... do something with lin > > What about > > >>> with open(path_string)

Re: The rap against "while True:" loops

2009-10-12 Thread Mel
Luis Zarrabeitia wrote: > On Sunday 11 October 2009 11:56:55 pm Dennis Lee Bieber wrote: >> In this situation, the middle exit works best -- using >> non-optimal Python >> >> while True: >> lin = file_object.readline() >> if not lin: break >> do something with lin > > Actually, in python, this wo

Re: The rap against "while True:" loops

2009-10-12 Thread Xavier Ho
On Mon, Oct 12, 2009 at 11:32 PM, Luis Zarrabeitia wrote: > Actually, in python, this works even better: > > for lin in iter(file_object.readline, ""): >... do something with lin > > What about >>> with open(path_string) as f: >>> for line in f: >>> # do something Cheers, X

Re: The rap against "while True:" loops

2009-10-12 Thread Luis Zarrabeitia
On Sunday 11 October 2009 11:56:55 pm Dennis Lee Bieber wrote: > In this situation, the middle exit works best -- using > non-optimal Python > > while True: > lin = file_object.readline() > if not lin: break > do something with lin Actually,

Re: The rap against "while True:" loops

2009-10-12 Thread greg
kj wrote: I'm coaching a group of biologists on basic Python scripting. One of my charges mentioned that he had come across the advice never to use loops beginning with "while True". It's possible this is something he was told in relation to another language that has more options. For example

Re: The rap against "while True:" loops

2009-10-12 Thread greg
Mensanator wrote: while not done: ... if n==1: done = True ... Seems to me that 'while not done:' is no better than 'while True:', because in both cases you have to look inside the loop to find out what the exit condition is. Using a more meaningful name for the flag can help, but

Re: The rap against "while True:" loops

2009-10-12 Thread greg
RDrewD wrote: my_prissy_little_indicator_variable = true while (my_prissy_little_indicator_variable){ } isn't satisfying because it doesn't guard the with any assurance that the loop invariant will be true before you enter into that block of code. The loop invariant and the exit conditio

Re: The rap against "while True:" loops

2009-10-12 Thread greg
Steven D'Aprano wrote: Back in ancient days when dinosaurs walked the Earth, and I was programming in THINK Pascal on Apple Macintosh System 6, I'd go into nervous palpitations writing the equivalent of "while True" because if I got it wrong, I'd lock up the machine and need to hit the power b

Re: The rap against "while True:" loops

2009-10-12 Thread Bearophile
Peter Billam: > I remember in the structured-programming revolution the >    loop { ... if whatever {break;} ... } > idiom was The Recommended looping structure, because the code is > more maintainable. I think "break" was almost the antithesis of structured programming, it was seen as the little

Re: The rap against "while True:" loops

2009-10-12 Thread Peter Billam
On 2009-10-11, Bearophile wrote: > Peter Billam: >> I remember in the structured-programming revolution the >>    loop { ... if whatever {break;} ... } >> idiom was The Recommended looping structure, because the code is >> more maintainable. > > I think "break" was almost the antithesis of structu

Re: The rap against "while True:" loops

2009-10-11 Thread Gabriel Genellina
En Sun, 11 Oct 2009 23:01:47 -0300, RDrewD escribió: On Oct 11, 6:46 pm, Philip Semanchuk wrote: On Oct 11, 2009, at 5:51 PM, bartc wrote: > Mensanator wrote: >> On Oct 10, 3:15 pm, kj wrote: >>> I'm coaching a group of biologists on basic Python scripting. One >>> of my charges mentioned th

Re: The rap against "while True:" loops

2009-10-11 Thread Gabriel Genellina
En Sun, 11 Oct 2009 19:46:06 -0300, Philip Semanchuk escribió: On Oct 11, 2009, at 5:51 PM, bartc wrote: Mensanator wrote: On Oct 10, 3:15�pm, kj wrote: I'm coaching a group of biologists on basic Python scripting. �One of my charges mentioned that he had come across the advice never to u

Re: The rap against "while True:" loops

2009-10-11 Thread RDrewD
On Oct 11, 6:46 pm, Philip Semanchuk wrote: > On Oct 11, 2009, at 5:51 PM, bartc wrote: > > > > > Mensanator wrote: > >> On Oct 10, 3:15 pm, kj wrote: > >>> I'm coaching a group of biologists on basic Python scripting. One > >>> of my charges mentioned that he had come across the advice never > >

Re: The rap against "while True:" loops

2009-10-11 Thread Philip Semanchuk
On Oct 11, 2009, at 5:51 PM, bartc wrote: Mensanator wrote: On Oct 10, 3:15�pm, kj wrote: I'm coaching a group of biologists on basic Python scripting. �One of my charges mentioned that he had come across the advice never to use loops beginning with "while True". �Of course, that's one way t

Re: The rap against "while True:" loops

2009-10-11 Thread Mensanator
On Oct 11, 4:51�pm, "bartc" wrote: > Mensanator wrote: > > On Oct 10, 3:15 pm, kj wrote: > >> I'm coaching a group of biologists on basic Python scripting. One > >> of my charges mentioned that he had come across the advice never > >> to use loops beginning with "while True". Of course, that's on

Re: The rap against "while True:" loops

2009-10-11 Thread bartc
Mensanator wrote: On Oct 10, 3:15�pm, kj wrote: I'm coaching a group of biologists on basic Python scripting. �One of my charges mentioned that he had come across the advice never to use loops beginning with "while True". �Of course, that's one way to start an infinite loop, but this seems hard

Re: The rap against "while True:" loops

2009-10-11 Thread Gabriel Genellina
En Sat, 10 Oct 2009 19:32:25 -0300, Björn Lindqvist escribió: I have many times screwed up "while True"-loops. When I thought I had a safe exit condition which turned out to be never reached in some rare corner cases. Leading to weird bugs with hanging threads. I have seen colleges screw up i

  1   2   >