code for Computer Language Shootout
Steven Bethard
steven.bethard at gmail.com
Wed Mar 16 16:05:38 EST 2005
Michael Spencer wrote:
> def output(seq, linelength = 60):
> if seq:
> iterseq = iter(seq)
> while iterseq:
> print "".join(islice(iterseq,linelength))
Worth noting: "while iterseq" only works because for this case, you have
a list iterator, which provides a __len__ method. This approach will
not work when you have an iterator that does not provide a __len__
method. For a nice infinite printing loop, try:
def gen():
yield '+'*100
yield '*'*100
output(gen())
STeVe
More information about the Python-list
mailing list