Looping [was Re: Python and the need for speed]

Ben Bacarisse ben.usenet at bsb.me.uk
Mon Apr 17 20:18:21 EDT 2017

Marko Rauhamaa <marko at pacujo.net> writes:

> Ben Bacarisse <ben.usenet at bsb.me.uk>:
>> Marko Rauhamaa <marko at pacujo.net> writes:
>>> What I notice in my numbers is that about one half of my while loops
>>> are "while True", and about a third of my loops are while loops.
>> I fo[u]nd the proportion on while True: loops surprising. Is there
>> something about Python that encourages that kind of loop?

(Thanks for th type correction.  There is also s/of/on/.)

> Here's a typical example of such a loop in Python (ver 2):
>     while True:
>         try:
>             snippet = os.read(rdfd, 1000)
>         except OSError as e:
>             if e.errno == errno.EAGAIN:
>                 return
>             raise
>         if not snippet:
>             break
>         self.stdout_snippets.append(snippet)

Thanks (and to Grant).  IO seems to be the canonical example.  Where
some languages would force one to write

  c = sys.stdin.read(1)
  while c == ' ':
      c = sys.stdin.read(1)

and an Algol-68 style language would permit one to write

  while (c := read char; c) do skip od

Python opts for

  while True:
     c = sys.stdin.read(1)
     if c != ' ': break

(Forgive the atypical choice of input primitive -- it's for illustrating
the loop style only.)


More information about the Python-list mailing list