Re: while True or while 1

2012-01-23 Thread Steven D'Aprano
On Mon, 23 Jan 2012 20:50:11 +, Andrea Crotti wrote: > while 1 works because the 1 is converted to boolean automatically, but > why not just writing a boolean > in the first place? You have misunderstood Python's truth model. It is similar to languages like Javascript and PHP, where EVERY ob

Re: while True or while 1

2012-01-23 Thread 88888 Dihedral
在 2012年1月24日星期二UTC+8上午4时50分11秒,Andrea Crotti写道: > On 01/23/2012 06:05 PM, Evan Driscoll wrote: > > > > To play devil's advocate for a moment, if you have the choice between > > two ways of writing something, A and B, where both are basically the > > same in terms of difficulty to write, difficult

Re: while True or while 1

2012-01-23 Thread Andrea Crotti
On 01/23/2012 06:05 PM, Evan Driscoll wrote: To play devil's advocate for a moment, if you have the choice between two ways of writing something, A and B, where both are basically the same in terms of difficulty to write, difficulty to maintain, and difficulty to understand, but A is faster t

Re: while True or while 1

2012-01-23 Thread Giampaolo Rodolà
Il 23 gennaio 2012 20:12, Erik Max Francis ha scritto: > Giampaolo Rodolà wrote: >> >> Il 21 gennaio 2012 22:13, Erik Max Francis ha scritto: >>> >>> The real reason people still use the `while 1` construct, I would >>> imagine, >>> >>> is just inertia or habit, rather than a conscious, defensive

Re: while True or while 1

2012-01-23 Thread Erik Max Francis
Giampaolo Rodolà wrote: Il 21 gennaio 2012 22:13, Erik Max Francis ha scritto: The real reason people still use the `while 1` construct, I would imagine, is just inertia or habit, rather than a conscious, defensive decision. If it's the latter, it's a case of being _way_ too defensive. It's

Re: while True or while 1

2012-01-23 Thread Evan Driscoll
On 01/23/2012 11:39 AM, Ian Kelly wrote: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil" -- Donald Knuth To play devil's advocate for a moment, if you have the choice between two ways of writing something, A and B, where bo

Re: while True or while 1

2012-01-23 Thread Dave Angel
On 01/23/2012 08:28 AM, Hrvoje Niksic wrote: Dave Angel writes: I do something similar when there's a portion of code that should never be reached: assert("reason why I cannot get here") Shouldn't that be assert False, "reason why I cannot get here"? You caught me in a typo. If it's in pyth

Re: while True or while 1

2012-01-23 Thread Ian Kelly
On Mon, Jan 23, 2012 at 9:41 AM, Giampaolo Rodolà wrote: > > Il 21 gennaio 2012 22:13, Erik Max Francis ha scritto: > > The real reason people still use the `while 1` construct, I would imagine, > > is just inertia or habit, rather than a conscious, defensive decision.  If > > it's the latter, it

Re: while True or while 1

2012-01-23 Thread Giampaolo Rodolà
Il 21 gennaio 2012 22:13, Erik Max Francis ha scritto: > The real reason people still use the `while 1` construct, I would imagine, > is just inertia or habit, rather than a conscious, defensive decision.  If > it's the latter, it's a case of being _way_ too defensive. It's also because while 1 i

Re: while True or while 1

2012-01-23 Thread Grant Edwards
On 2012-01-21, Erik Max Francis wrote: > Chris Angelico wrote: >> On Sun, Jan 22, 2012 at 12:47 AM, Andrea Crotti >> wrote: >>> So I tried to do the following, and the result is surprising. For what >>> I can see it looks like the interpreter can optimize away the 1 boolean >>> conversion while

Re: while True or while 1

2012-01-23 Thread Hrvoje Niksic
Dave Angel writes: > I do something similar when there's a portion of code that should > never be reached: > > assert("reason why I cannot get here") Shouldn't that be assert False, "reason why I cannot get here"? -- http://mail.python.org/mailman/listinfo/python-list

Re: while True or while 1

2012-01-23 Thread Dave Angel
On 01/22/2012 10:55 PM, alex23 wrote: On Jan 23, 2:05 am, Dan Sommers wrote: As per a now-ancient suggestion on this mailing list (I believe it was by Tim Peters), I've also been known to use a non-empty, literal Python string as a self-documenting, forever-True value in the typical loop-and-a-

Re: while True or while 1

2012-01-22 Thread alex23
On Jan 23, 2:05 am, Dan Sommers wrote: > As per a now-ancient suggestion on this mailing list (I believe it was by > Tim Peters), I've also been known to use a non-empty, literal Python > string as a self-documenting, forever-True value in the typical loop-and-a- > half cnstruct: > >     while "th

Re: while True or while 1

2012-01-22 Thread MRAB
On 22/01/2012 16:05, Dan Sommers wrote: On Sun, 22 Jan 2012 05:25:25 +, Steven D'Aprano wrote: Or they've been writing Python code since before version 2.2 when True and False were introduced, and so they are used to the "while 1" idiom and never lost the habit. That would be me. As p

Re: while True or while 1

2012-01-22 Thread Dan Sommers
On Sun, 22 Jan 2012 05:25:25 +, Steven D'Aprano wrote: > Or they've been writing Python code since before version 2.2 when True > and False were introduced, and so they are used to the "while 1" idiom > and never lost the habit. That would be me. As per a now-ancient suggestion on this maili

Re: while True or while 1

2012-01-21 Thread Steven D'Aprano
On Sun, 22 Jan 2012 09:13:23 +1100, Chris Angelico wrote: > On Sun, Jan 22, 2012 at 8:13 AM, Erik Max Francis > wrote: >> Why this should concern anyone, I don't know; someone who's rebound >> `True` or `False` to evaluate to something other than true and false, >> respectively, is only doing so

Re: while True or while 1

2012-01-21 Thread Chris Angelico
On Sun, Jan 22, 2012 at 8:13 AM, Erik Max Francis wrote: > Why this should concern anyone, I don't know; someone who's rebound `True` > or `False` to evaluate to something other than true and false, respectively, > is only doing so to be difficult (or very foolish).  One of the principles > of Pyt

Re: while True or while 1

2012-01-21 Thread Erik Max Francis
Andrea Crotti wrote: I see sometimes in other people code "while 1" instead of "while True". I think using True is more pythonic, but I wanted to check if there is any difference in practice. No (with the exception of `True` and `False` being rebinable in Python 2). The idiomatic `while 1` no

Re: while True or while 1

2012-01-21 Thread Erik Max Francis
Chris Angelico wrote: On Sun, Jan 22, 2012 at 12:47 AM, Andrea Crotti wrote: So I tried to do the following, and the result is surprising. For what I can see it looks like the interpreter can optimize away the 1 boolean conversion while it doesn't with the True, the opposite of what I supposed

Re: while True or while 1

2012-01-21 Thread Matteo Landi
Probably because of the fact it is possible to set True equal to False and consequently then invalidate loop logic as presented below: True = False while True: ... On the other hand `1' will always be evaluated as a constant. Don't know, just guessing. Matteo On Jan/21, Andrea

Re: while True or while 1

2012-01-21 Thread Chris Angelico
On Sun, Jan 22, 2012 at 12:47 AM, Andrea Crotti wrote: > So I tried to do the following, and the result is surprising.  For what > I can see it looks like the interpreter can optimize away the 1 boolean > conversion while it doesn't with the True, the opposite of what I > supposed. > > Anyone can

Re: while True or while 1

2012-01-21 Thread Andrea Crotti
Actually there was the same question here (sorry should have looked before) http://stackoverflow.com/questions/3815359/while-1-vs-for-whiletrue-why-is-there-a-difference And I think the main reason is that 1 is a constant while True is not such and can be reassigned. -- http://mail.python.org/mai

while True or while 1

2012-01-21 Thread Andrea Crotti
I see sometimes in other people code "while 1" instead of "while True". I think using True is more pythonic, but I wanted to check if there is any difference in practice. So I tried to do the following, and the result is surprising. For what I can see it looks like the interpreter can optimize a

Re: while True or while 1

2010-12-28 Thread Francesco
hehehehehehe... On 17/12/2010 2.01, Steven D'Aprano wrote: On Thu, 16 Dec 2010 23:34:21 +, BartC wrote: In terms of a more realistic function (admittedly still a little contrived, as the loop would be written differently), I tried this: def p2(n): p=1 while True: if n<=p: retur

Re: while True or while 1

2010-12-16 Thread Steven D'Aprano
On Thu, 16 Dec 2010 23:34:21 +, BartC wrote: > In terms of a more realistic function (admittedly still a little > contrived, as the loop would be written differently), I tried this: > > def p2(n): > p=1 > while True: > if n<=p: return p > p<<=1 > return 0 > > for i in xrange(10

Re: while True or while 1

2010-12-16 Thread Ian
On Dec 16, 4:34 pm, "BartC" wrote: > def p2(n): >   p=1 >  whileTrue: >     if n<=p: return p >     p<<=1 >   return 0 > > for i in xrange(100): >   x=p2(i) > > p2() calculates the smallest power of 2 >= it's operand. def p2(n): return 1 << n.bit_length() -- http://mail.python.org/mailman/

Re: while True or while 1

2010-12-16 Thread BartC
"Steve Holden" wrote in message news:mailman.54.1292502247.6505.python-l...@python.org... On 12/16/2010 5:44 AM, BartC wrote: One these is 30% faster than the other. That's an appreciable difference, which you can't really just dismiss. shol...@lifeboy ~ $ python -m timeit -- "i = 1" "whi

Re: while True or while 1

2010-12-16 Thread Ethan Furman
Arnaud Delobelle wrote: Ethan Furman writes: ...I timed exec vs function, and found the function style to be about 200% faster... So it finished before it started? Hmmm Let me check my calculator... . . . Ah! Okay, that was 200x faster. :) I think -- it was a few months ago no

Re: while True or while 1

2010-12-16 Thread Terry Reedy
On 12/16/2010 7:23 AM, Steve Holden wrote: On 12/16/2010 5:44 AM, BartC wrote: One these is 30% faster than the other. That's an appreciable difference, which you can't really just dismiss. And you can't tell what the overall effect on a program will be: perhaps the loop will be in a library

Re: while True or while 1

2010-12-16 Thread Arnaud Delobelle
Ethan Furman writes: > ...I timed exec vs function, and found the function style to be about > 200% faster... So it finished before it started? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: while True or while 1

2010-12-16 Thread Ethan Furman
BartC wrote: "Steve Holden" wrote in message news:mailman.462.1292214062.2649.python-l...@python.org... On 12/12/2010 2:32 PM, Christian Heimes wrote: Am 12.12.2010 19:31, schrieb Steve Holden: $ python -m timeit -n20 -- "i = 0" "while 1:" "i+=1" "if i == 100: break" 20 loops, best

Re: while True or while 1

2010-12-16 Thread Steve Holden
On 12/16/2010 5:44 AM, BartC wrote: >> On 12/12/2010 2:32 PM, Christian Heimes wrote: >>> Am 12.12.2010 19:31, schrieb Steve Holden: >>> $ python -m timeit -n20 -- "i = 0" "while 1:" "i+=1" "if i == >>> 100: break" >>> 20 loops, best of 3: 89.7 msec per loop >>> $ python -m timeit -n20

Re: while True or while 1

2010-12-16 Thread BartC
"Steve Holden" wrote in message news:mailman.462.1292214062.2649.python-l...@python.org... On 12/12/2010 2:32 PM, Christian Heimes wrote: Am 12.12.2010 19:31, schrieb Steve Holden: $ python -m timeit -n20 -- "i = 0" "while 1:" "i+=1" "if i == 100: break" 20 loops, best of 3: 89.7 ms

Re: while True or while 1

2010-12-15 Thread Steve Holden
On 12/15/2010 8:10 AM, Hans-Peter Jansen wrote: > On Tuesday 14 December 2010, 21:38:47 Arnaud Delobelle wrote: >> Christian Heimes writes: >> [...] >> >>> Tres Seavers once told me a joke like this: >>> >>>True = not not "Who's at the door?" # say it out loud! >>> >>> This was back in the old

Re: while True or while 1

2010-12-15 Thread Hans-Peter Jansen
On Tuesday 14 December 2010, 21:38:47 Arnaud Delobelle wrote: > Christian Heimes writes: > [...] > > > Tres Seavers once told me a joke like this: > > > >True = not not "Who's at the door?" # say it out loud! > > > > This was back in the old days of Zope 2.5 and Python 2.1, which > > didn't ha

Re: while True or while 1

2010-12-15 Thread bruno.desthuilli...@gmail.com
On 14 déc, 21:38, Arnaud Delobelle wrote: > I almost used: > >     True = "to be" or not "to be" # that is the question KEYBOARD !-) -- http://mail.python.org/mailman/listinfo/python-list

Re: while True or while 1

2010-12-14 Thread Arnaud Delobelle
Christian Heimes writes: [...] > Tres Seavers once told me a joke like this: > >True = not not "Who's at the door?" # say it out loud! > > This was back in the old days of Zope 2.5 and Python 2.1, which didn't > have True and False. I almost used: True = "to be" or not "to be" # that is

Re: while True or while 1

2010-12-14 Thread Christian Heimes
Am 14.12.2010 17:52, schrieb Arnaud Delobelle: > You also need to initialise False to False for it to be really > robust. So something like this will do. > > True = not 0 > False = not True > while True: > ... > True = False Tres Seavers once told me a joke like this:

Re: while True or while 1

2010-12-14 Thread Arnaud Delobelle
Gregory Ewing writes: > Steven D'Aprano wrote: > >while True: >> >> ... print "Looping" >> ... True = 0 > > Just remember that if you use that inside a function, you'll > have to initialise True to True before... er, wait a moment, > that won't work... ah, I know: > > def f(true = T

Re: while True or while 1

2010-12-14 Thread Kurt Mueller
Am 14.12.2010 11:33, schrieb Hans-Peter Jansen: > On Tuesday 14 December 2010, 10:19:04 Gregory Ewing wrote: >> Steven D'Aprano wrote: >> while True: >>> >>> ... print "Looping" >>> ... True = 0 >> >> Just remember that if you use that inside a function, you'll >> have to initialise True to Tru

Re: while True or while 1

2010-12-14 Thread Hans-Peter Jansen
On Tuesday 14 December 2010, 10:19:04 Gregory Ewing wrote: > Steven D'Aprano wrote: > while True: > > > > ... print "Looping" > > ... True = 0 > > Just remember that if you use that inside a function, you'll > have to initialise True to True before... er, wait a moment, > that won't wor

Re: while True or while 1

2010-12-14 Thread Gregory Ewing
Steven D'Aprano wrote: while True: ... print "Looping" ... True = 0 Just remember that if you use that inside a function, you'll have to initialise True to True before... er, wait a moment, that won't work... ah, I know: def f(true = True): True = true while True: ..

Re: while True or while 1

2010-12-13 Thread Grant Edwards
On 2010-12-12, Steven D'Aprano wrote: > With the "while True" idiom in Python 2.x, you can easily exit out of an > infinite loop without using break: > while True: > ... print "Looping" > ... True = 0 > ... > Looping while True: # Execute an infinite loop in 0 seconds. >

Re: while True or while 1

2010-12-13 Thread Jean-Michel Pichavant
Paul Rubin wrote: Steven D'Aprano writes: I'm actually quite fond of the look of "while 1:", and sometimes use it, not because it's faster, but just because I like it. for v in itertools.repeat(True): ... ;-) while '__For_ever___' not in ['nit-picking']: :) JM -- http

Re: while True or while 1

2010-12-12 Thread Paul Rubin
Steven D'Aprano writes: > I'm actually quite fond of the look of "while 1:", and sometimes use it, > not because it's faster, but just because I like it. for v in itertools.repeat(True): ... ;-) -- http://mail.python.org/mailman/listinfo/python-list

Re: while True or while 1

2010-12-12 Thread Steven D'Aprano
On Sun, 12 Dec 2010 23:20:40 -0500, Steve Holden wrote: > On 12/12/2010 2:32 PM, Christian Heimes wrote: [...] >> No argue with that! I was merely making a point that "while 1" executes >> different byte code than "while True". Readability is important but >> sometimes speed is of the essence. "wh

Re: while True or while 1

2010-12-12 Thread Steve Holden
On 12/12/2010 2:32 PM, Christian Heimes wrote: > Am 12.12.2010 19:31, schrieb Steve Holden: >> > Would you care to quantify how much CPU time that optimization will >> > typically save for a loop of fair magnitude (say, a billion iterations)? > The difference is minimal but measurable for very tigh

Re: while True or while 1

2010-12-12 Thread Steven D'Aprano
On Sun, 12 Dec 2010 16:33:41 +0100, Krister Svanlund wrote: > On Sun, Dec 12, 2010 at 3:14 PM, Max Countryman wrote: >> I'm sure this has been brought up many times, but a quick Googling >> didn't yield the decisive results I was hoping for, so I apologize if >> this has already been addressed in

Re: while True or while 1

2010-12-12 Thread Martin v. Loewis
>> Python is designed to provide readable code. Writing >> >> while True: >> ... >> >> is much more legible than its pre-True couterpart >> >> while 1: >> ... > > No argue with that! I actually want to argue with that: I find "while 1" more legible. That's probably because

Re: while True or while 1

2010-12-12 Thread Christian Heimes
Am 12.12.2010 19:31, schrieb Steve Holden: > Would you care to quantify how much CPU time that optimization will > typically save for a loop of fair magnitude (say, a billion iterations)? The difference is minimal but measurable for very tight loops. $ python -m timeit -n20 -- "i = 0" "while 1:"

Re: while True or while 1

2010-12-12 Thread Steve Holden
On 12/12/2010 10:30 AM, Christian Heimes wrote: > Am 12.12.2010 15:14, schrieb Max Countryman: >> I'm sure this has been brought up many times, but a quick Googling didn't >> yield the decisive results I was hoping for, so I apologize if this has >> already been addressed in great detail somewher

Re: while True or while 1

2010-12-12 Thread Krister Svanlund
On Sun, Dec 12, 2010 at 3:14 PM, Max Countryman wrote: > I'm sure this has been brought up many times, but a quick Googling didn't > yield the decisive results I was hoping for, so I apologize if this has > already been addressed in great detail somewhere else. > > I am wondering what the ration

Re: while True or while 1

2010-12-12 Thread Christian Heimes
Am 12.12.2010 15:14, schrieb Max Countryman: > I'm sure this has been brought up many times, but a quick Googling didn't > yield the decisive results I was hoping for, so I apologize if this has > already been addressed in great detail somewhere else. > > I am wondering what the rationale is beh

while True or while 1

2010-12-12 Thread Max Countryman
I'm sure this has been brought up many times, but a quick Googling didn't yield the decisive results I was hoping for, so I apologize if this has already been addressed in great detail somewhere else. I am wondering what the rationale is behind preferring while True over while 1? For me, it see