why no "do : until"?
Tom Satter
tsatter at purecode.com
Tue Jan 9 13:15:33 EST 2001
Looking at the Indexing class gave me an idea, what about having
a class 'Popable' that would let you pop items out of a loop while
in the loop:
class Popable:
def __init__(self, thing):
self.thing = thing
self.last = -1
self.offset = 0
def __getitem__(self, index):
self.last = index
return self.thing[index+self.offset]
def pop(self):
self.offset += 1
return self[self.last]
a = "abcdefghijklmnopqrstuvwxyz"
l = Popable(a)
for x in l:
if x == 'a' or x == 'e' or x == 'i' or x == 'o' or x == 'u':
x = l.pop()
print x
This would print all of the letters in the alphabet except
the vowels. I realize that there would be some handling
necessary if one tried to pop the last element of the list
but it may be a good start for some of the command line
argument handling code that has been requested.
--
tom -- just plain old tom
tsatter at purecode.com
"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:93dfg1177p at news1.newsguy.com...
> "David Morley" <morley at Borokay.ai.sri.com> wrote in message
> news:fvy9wlztq2.fsf at Borokay.AI.SRI.COM...
> [snip]
> > What would be wrong with:
> >
> > class Indexing:
> > def __init__(self, thing):
> > self.thing = thing
> > def __getitem__(self, index):
> > return (index, self.thing[index])
> >
> > for (i,x) in Indexing(a):
>
> Just the redundant parentheses --
> for i, x in Indexing(a):
> reads fractionally more smoothly (and similarly,
> the parentheses are not needed on the return
> from __getitem__). Pretty cool idea though!-)
>
> Alex
More information about the Python-list
mailing list