Message46134
Logged In: YES
user_id=1058331
The OP's use case was too simplified to convey
the usefulness of fileinput.islastline(). It does not
just save a couple of lines. I am attaching a "wc" or
"word count" program to demonstrate. Note the following
problems with the current work-around:
1. Instead of doing end-of-file processing at the end of
a file, we must do it at the beginning of the *next*
file.
2. Useful functions such as fileinput.filelineno() and
fileinput.filename() now refer to the *wrong* file,
so now we need to manually keep track of these values
ourselves.
3. Perhaps a minor point, but this work-around only works
at the *beginning* of the loop instead of the end. It
feels somewhat awkward.
4. End-of-file processing must occur both in the loop and
once outside. A function avoids code duplication, but
to refer to variables in the loop, they must be passed
into and out of the function (6 in, 2 out for the "wc"
example... maybe keyword arguments would have helped).
5. Most important is readablility. It's not obvious that
`fileinput.isfirstline() and fileinput.lineno() != 1`
means we are the end of the (previous!) file. Using
`fileinput.islastline()` would be much clearer.
And not that it is a reason, but Perl and Ruby solve
this with an `eof` function and `eof?` method,
respectively.
|
|
Date |
User |
Action |
Args |
2007-08-23 15:38:03 | admin | link | issue968063 messages |
2007-08-23 15:38:03 | admin | create | |
|