Graceful handling of first line

Follower follower at gmail.com
Thu Oct 14 05:29:39 EDT 2004


Egbert Bouwman <egbert.list at hccnet.nl> wrote in message news:<mailman.4563.1097227579.5135.python-list at python.org>...
> Of course it is not difficult to test if you are reading the first line
> or another one, but it hurts my feelings to do a test which by definition
> succeeds at the first record, and never afterwards.
> Any suggestions ?
An alternative approach (which I'm sure will offend just as many
sensibilities) is to use a function that replaces itself.

------ Pseudo-code ------


def process_firstline(...):

   # ...do something here...
   global processline
   processline = process_otherlines

 
def process_otherlines(...):

   # ...do something here...


processline = process_firstline

for line in file:
   result = processline(line)

----------------------------

If you read more than one file you'll need to reset processline at the
beginning of each file.

I never said this was a *good* way, just one way. :-)

--Phil.



More information about the Python-list mailing list