[Tutor] Readlines

Adam Vardy Adam Vardy <anvardy@roadrunner.nf.net>
Thu Jun 26 14:39:02 2003


Wednesday, June 25, 2003, 4:58:12 PM, you wrote:

>> While its true that most languages have a numerically oriented FOR
>> loop, many also have a FOREACH loop construct. Python just
>> happens to call its FOREACH construct FOR...

>> So it would be neater if Python kept to that tradition.
>> and had an implied counter for any structure like that,

>> Why? It's not that often you need to know the count if you are
>> processing everything in a list - which is what the FOR loop is for!

Rodrigues suggested the basic kind of feature was recently added.

>> I was thinking you could probably access the counter out of
>> >> for line in f.xreadlines():
>> >>     print line
>>
>> Since, to do what you're asking of it, it has to be keeping
>> track with exactly this kind of integer counter.

>> Why? What makes you think that. It could just keep processing things
>> till it runs out of items to process. It doresn't need to keep a
>> counter.

I know you're the expert Alan, but you might not have left high level
concepts for the moment.

Just that, suppose you only have four months in a year. The computer
would probably have:

00 00- January.
01 01- February.
02 10- March
03 11- April

It won't bother thinking about a February. If it counts through the
months, it won't conceive of time, metaphors, or abstract items. Just
add one each time.  It will have to do that.

I was just saying like, sometimes you add variables a lot, and maybe
some languages that are more abstract often get along with fewer.

You're adding LineCount wasn't really more confusing.

But, you might to do that but be less descriptive, and say 'lc' or 'l'.
And you won't know what it was later. Think so?

If you just have 'line' you could use it both for the printed line,
and the number of that line. Both fit well trying to read like plain
English.

Are you sure you've never added counters before in lots of places
around, and then left very little working code on one screen/page?

>> Consider the common case of processing a file with a while loop
>> until you reach the end of file... You don't need to track the
>> line number.

Probably not, if there are functions to read from the start, or end of
a file.

>> When you have to add extra variables, it just adds more text on your
>> screen, and less space to review the main stuff you've actually
>> written.

>> For the relatively few cases where you need an index counter then
>> the amount of extra text is small, and you have the choice of using
>> a while loop, or for loop. Mostly when you use a for loop you just
>> want to process each item.

Except when you're debugging, you probably want to print just about
anything.

Like when a function doesn't seem to be running correctly.

-- 
Adam Vardy