A single, general looping construct? (was: why no "do : until"?)

Steve Holden sholden at holdenweb.com
Tue Jan 9 16:37:09 EST 2001


Pawel Kedzierski <kedziers at pkmk486.ch.pwr.wroc.pl> wrote in message
news:93euo1$drh$1 at okapi.ict.pwr.wroc.pl...
> Hi there,
>
>   I become a python enthusiast a short time ago and even I adopted it
> as a programming language for my classes on programming in chemistry.
> It allowed to shift the balance of training/applications largery to
> the right side :-); However, I also found hard to explain why there
> isn't any repeat ... until replacement in Python.
>
>   So it is why I am here :-). The actual hot discussion i have read
> on this thread and before indicates that although the topic was discussed
> thorough the whole Python lifetime. So I'm not going to add any more
> pros as there were so many mentioned. I simply come with an idea,
> which seems attractive, so let's have it discussed.
>
>   As far as I understand, the main reason against another loop construct
> is the need to introduce a new keyword. Here is a solution, which doesn't
> need it. And, IMHO, it is simple, elegant yet powerful:
>
> Let's extend the meaning of try and while.
>
> The try: statement could begin both the exception control and the loop.
> Of course, in this case either the except clause or the while one would
> be optional.
> I would especially like constructs like this:
>
> try:
>   f = raw_input('Data file: ')
>   f = open(f,'r')
> except IOError:
>   print 'Could not open file'
> while not IOError
>
> What do You all think about it?
>
> Best regards
>
>         Pawel

Well the first observation is that nothing in your example actually
processes the content of each file.  Perhaps I can try to extend it in the
same spirit to see what the code would look like actually processing the
files...

try:
  f = raw_input('Data file: ')
  f = open(f,'r')
  try:
    l = f.readline()
    # do some processing on the line
  while l != ""
except IOError:
  print 'Could not open file'
while not IOError

In this example, no exception would be raised when the end of file is
reached. The readline() method simply returns an empty string.

The thing I don't like is that the test appears at the end of the loop but
actually needs to be perfomed halfway through it, after the readline() call
has been processed. This, I suspect, is the main reason why Alex Martelli
has returned so often to remind us about the Knuthian "n-and-a-hlf-times"
loop construct. (Alex: if I'm misrepresenting you here, do me a favor and
just let it go by this once, eh? &v)

Also, in both your original example and my extension, the final "while"
statement has a condition which isn't really a Boolean. You appear to mean
"as long as a particular exception has not been raised", but exceptions
don't really work like that. You would end up having to clear a flag before
the loop, and set it when the exception occurs:

flag = 0
try:
  filename = raw_input('Data file: ')
  f = open(filename, 'r')
  # Process the file
except IOError:
  print "Could not open file", filename
  flag = 1
while not flag

which does not seem to be a real improvement over

flag = 0
while not flag:
  try:
    filename = raw_input('Data file: ')
    f = open(filename, 'r')
    # Process the file
  except IOError:
    print "Could not open file", filename
    flag = 1

Also, of course, one might ask how this loop is intended to terminate
"normally"? Do you enter a non-existent filename, a blank line, and
end-of-file? How would this be taken into account, since no condition in
your pseudo-code addresses this question?

I hope I'm not being too critical here. I have nothing against attempts to
improve the Python language (despite the endless threads such attempts
generate). It says a lot for Python that so many people want to improve it
still further.  Just the same, I have a feeling that the language as it
stands is good enough that improving it further is a non-trivial task.

regards
 Steve





More information about the Python-list mailing list