Iterating over a binary file

Paul Rubin http
Tue Jan 6 19:09:13 EST 2004


Ville Vainio <ville.vainio at spamster_tut_remove.fi> writes:

> Paul Rubin <http://phr.cx@NOSPAM.invalid> writes:
>
> > > The above code works, but I don't like making two read() calls.  Any
> > > way to avoid it, or a more elegant syntax?  Thanks.
> >
> > You can make it even uglier:
> >
> >     f = file(filename, 'rb')
> >     while 1:
> >       data = f.read(1024)
> >       if len(data) <= 0:
> >         break
> >       someobj.update(data)
> >     f.close()
> >
> > There's been proposals around to add an assignment-expression operator
> > like in C, so you could say something like
> >
> >     f = file(filename, 'rb')
> >     while len(data := f.read(1024)) > 0:
> >       someobj.update(data)
> >     f.close()
>
> It's funny, but I find the first version much more readable than the
> second one. Especially if I consciously forget the "do lots of stuff
> in condition part of while" indoctrination from C. If there is lots of
> stuff in while you have to stare at it a bit more, and it becomes
> "idiomatic", something you learn, perhaps even cookbook stuff, instead
> of obvious-as-such.

Idioms exist because they're useful, and there's already plenty of
them in Python, like ''.join(stringlist) or "for i in xrange(n)" etc.

Maybe the condition in the while statement makes that statement twice
as hard to read.  However, the example as a whole can still be easier,
simply because it's shorter.

Version 1:

    Statement                              Reading difficulty
    =========                              ==================

      f = file(filename, 'rb')                     1
      while 1:                                     1
        data = f.read(1024)                        1
        if len(data) <= 0:                         1
          break                                    1
        someobj.update(data)                       1
      f.close()                                    1

    Total reading difficulty:                      7

Now the second version:

    Statement                              Reading difficulty
    =========                              ==================

       f = file(filename, 'rb')                    1
       while len(data := f.read(1024)) > 0:        2
         someobj.update(data)                      1
       f.close()                                   1


    Total reading difficulty:                      5

I got through college on a version of this reasoning.  I was a math
major.  I had friends studying history and literature who said "that's
a hard subject", but I thought they were crazy.  But in a normal math
class, there's one textbook that you use for the whole semester, and
you cover maybe half the chapters in it.  I was able to keep up.  But
in a literature course, you usually have to read a different entire
book from cover to cover EVERY WEEK.  I took a couple classes like
that and barely survived.  Yes, it takes a lot more effort to read a
page of a math book than a page of a novel.  When you compare the
total reading load though, math was a much easier major than
literature or history.  

It's the same with programs.  I'd rather read 5 lines of tight code
that each actually does something, than 3 pages of loose code (the
kind that's usually written in Java) that spastically meanders trying
to do the same thing, even if the individual loose lines are easier to
read than the tight lines.



More information about the Python-list mailing list