Loop-and-a-half (Re: Curious assignment behaviour)
Quinn Dunkan
quinn at hork.ugcs.caltech.edu
Thu Oct 11 23:18:20 EDT 2001
On 11 Oct 2001 12:48:28 -0700, Paul Rubin <phr-n2001d at nightsong.com> wrote:
>slinkp23 at yahoo.com (Paul Winkler) writes:
>> That's not a very compelling example anymore. Presumably you don't
>> want to do "for line in file.readlines()" because of the memory
>> implications of slurping the whole file? Well, as of python 2.1 we can
>> get one line at a time like this:
>>
>> for line in file.xreadlines():
>> ...
>
>What if you want to read until you get to a delimiter?
>
> while (line := readline()) != 'end': ...
for line in iter(open('file').readline, 'end\n'):
...
In programming you define functions to capture your patterns. I'd rather
write a function than redesign the language. And in this case, the function
is already written (not that I expected you to know that since it's new).
I've never felt the need for loops with more eyebrows. I'd rather define
a higher level abstraction and keep the loop as a simple-as-possible
implementation detail. If we had a nicer syntax for generators we could get
rid of while entirely and have only one looping construct:
def while(p):
if not p: fail()
loop: # loops until top generator fails
while(x == y) # fails if value is false
...
loop:
line = open('foo').readline() # fails when out of lines
if line.startswith('#'): fail() # fails immediately
word = line.split() # fails when out of words
...
Of course this wouldn't be python any more (or even icon). When I write my
own language... :)
More information about the Python-list
mailing list