Re: while True or while 1

Discussion in 'Python' started by Krister Svanlund, Dec 12, 2010.

  1. 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 rationale is behind preferring while True over while 1? For me, it seems that using True provides more clarity, but is that the only benefit? Is while 1 more prone to errors?


    It's just silly to use 1 since it will evaluate to True either way.
     
    Krister Svanlund, Dec 12, 2010
    #1
    1. Advertising

  2. 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 great detail somewhere else.
    >>
    >> I am wondering what the rationale is behind preferring while True over
    >> while 1? For me, it seems that using True provides more clarity, but is
    >> that the only benefit? Is while 1 more prone to errors?

    >
    > It's just silly to use 1 since it will evaluate to True either way.



    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.

    .... print "Looping"
    ....
    >>>



    *wink*



    --
    Steven
     
    Steven D'Aprano, Dec 12, 2010
    #2
    1. Advertising

  3. 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.

    > ... print "Looping"
    > ...
    >>>>

    >
    > *wink*


    Sadly, I've seen people do stuff like that in real programs...

    --
    Grant Edwards grant.b.edwards Yow! I was making donuts
    at and now I'm on a bus!
    gmail.com
     
    Grant Edwards, Dec 13, 2010
    #3
  4. 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:
    ...
    True = False

    --
    Greg
     
    Gregory Ewing, Dec 14, 2010
    #4
  5. 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 work... ah, I know:
    >
    > def f(true = True):
    > True = true
    > while True:
    > ...
    > True = False


    Thankfully, with Python 3 this code falls flat on its face.

    If I would have to _consume_ code like that more often,
    it would require me to also use a vomit resistant keyboard cover..

    Pete
     
    Hans-Peter Jansen, Dec 14, 2010
    #5
  6. Krister Svanlund

    Kurt Mueller Guest

    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 True before... er, wait a moment,
    >> that won't work... ah, I know:
    >>
    >> def f(true = True):
    >> True = true
    >> while True:
    >> ...
    >> True = False

    >
    > Thankfully, with Python 3 this code falls flat on its face.
    >
    > If I would have to _consume_ code like that more often,
    > it would require me to also use a vomit resistant keyboard cover..
    >
    > Pete



    True yesterday, today and in the future:


    Yesterday:
    "Pilate said to him, True? what is true?
    Having said this he went out again to the Jews
    and said to them, I see no wrong in him."

    Today:
    We are so thankful that today we are free
    to define "True" ourselves using Python 2.x.

    Future:
    Be warned, the future gets darker!


    ;-)


    Grüessli
    --
    Kurt Mueller
     
    Kurt Mueller, Dec 14, 2010
    #6
  7. 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 = True):
    > True = true
    > while True:
    > ...
    > True = False


    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

    :)

    --
    Arnaud
     
    Arnaud Delobelle, Dec 14, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timothy Fitz

    while 1 vs while True

    Timothy Fitz, Dec 13, 2004, in forum: Python
    Replies:
    16
    Views:
    649
    Terry Reedy
    Dec 15, 2004
  2. bdb112
    Replies:
    45
    Views:
    1,426
    jazbees
    Apr 29, 2009
  3. BartC

    Re: while True or while 1

    BartC, Dec 16, 2010, in forum: Python
    Replies:
    8
    Views:
    291
    Francesco
    Dec 28, 2010
  4. Andrea Crotti

    while True or while 1

    Andrea Crotti, Jan 21, 2012, in forum: Python
    Replies:
    0
    Views:
    168
    Andrea Crotti
    Jan 21, 2012
  5. Chris Angelico

    Re: while True or while 1

    Chris Angelico, Jan 21, 2012, in forum: Python
    Replies:
    16
    Views:
    506
    Steven D'Aprano
    Jan 24, 2012
Loading...

Share This Page