No Do while/repeat until looping construct in python?

Robert Amesz sheershion at mailexpire.com
Sun Mar 16 09:56:52 EST 2003


Frank Millman wrote:

> "Greg Ewing (using news.cis.dfn.de)" <me at privacy.net> wrote in
> message news:<b4rkcq$22tehe$1 at ID-169208.news.dfncis.de>... 
> 
> [snip]
>> 
>> And this is *such* a common looping pattern that I really
>> don't understand why *no* language I can remember seeing has
>> explicit support for it. 
>
> [snip]

For what it's worth, I would welcome extending the Python loop contruct 
to something like Alex suggested. I feel that it is much more legible 
than the standard while 1: idiom. (Not to mention it being ugly.) I 
would prefer a loop to have a single, clearly visible exit point, and 
break should be used in those cases the loop needs more than one exit 
point. (Which is not very often, in my experience.)

But, *sigh*, this is an old discussion, and the points of view seem 
pretty much fixed and frozen, so unless Guido springs a suprise on us 
things will stay as they are, except that we'll be writing while True: 
in future.

 
> [snip]
>
> One last bit of nostalgia, and something that I do miss a bit. I
> think the underlying architectures are too different to do
> anything about it, and many people will disapprove of it, but here
> it is anyway. 
> 
> Pick has something similar to lists, which it calls dynamic
> arrays. I will use Python syntax to illustrate what it could do.
> If you had a list "x" of three elements, and said "x[5] = 123",
> Pick would not give an error, it would automatically extend the
> list to a length of 6, making the 4th and 5th elements blank. This
> made for a very free-thinking programming style.

Ah, well, how about this, then:

class DynList(list):
    def __setitem__(self, index, value):
        try:
            list.__setitem__(self, index, value)
            
        except IndexError:
            if index < 0:
                raise
            self.extend([None] * (index - len(self)))
            self.append(value)


It never ceases to amaze me how easy some things can be in Python.

 
> I am not trying to start a debate here, so please do not respond
> to this.

No debate? On USENET!? Now there's a novel idea...


Robert Amesz




More information about the Python-list mailing list