The "loop and a half"

Terry Reedy tjreedy at udel.edu
Wed Oct 4 19:56:31 EDT 2017


On 10/4/2017 1:24 PM, Grant Edwards wrote:
> On 2017-10-04, Steve D'Aprano <steve+python at pearwood.info> wrote:
> 
>>> It is sometimes called the loop and a half problem. The idea is
>>> that you must attempt to read a line from the file before you know
>>> whether you are at the end of file or not.
>>
>> Utter nonsense. There's no "must" here. I'll accept the remote
>> possibility that maybe one time in a million you have to do what he
>> says, but the other 999999 times you just read from the file in a
>> for-loop:
>>
>> for line in file:
>>      process(line)
>>
>>> This can also be done if a boolean variable is introduced to help
>>> with the while loop. This boolean variable is the condition that
>>> gets you out of the while loop and the first time through it must be
>>> set to get your code to execute the while loop at least one."
>>
>> I've been programming in Python for twenty years, and I don't think I have
>> ever once read from a file using a while loop.
> 
> You're right, that construct isn't used for reading from files in
> Python.  It _is_ commonly used for reading from things like socket

> mysock.connect(...)
> while True:
>      data = mysock.recv(9999)
>      if not data:
>          break
>      do_something_with(data)
> mysock.close()

I contend that it would be better in this case also to separate data 
access from data processing.

def sockreader(socket, size):
     while True:
     data = socket.recv(size)
     if data:
         yield data
     else:
         break

for data in socketreader(mysock, 9999):
     do_something_with(data)

Perhaps the socket module needs an update to make the boilerplate 
generator function a method, such as 'iterread'.

The separation lets one write source-agnostic processing functions.

def do_something(source):
     for data in source:
         <code that implements 'do-something'>

One can now 'do_something' with data from pipe, file, socket, or list.
-- 
Terry Jan Reedy





More information about the Python-list mailing list