generators improvement

Steven Taschuk staschuk at telusplanet.net
Wed Aug 20 01:27:13 EDT 2003


Quoth Oleg Leschov:
  [...]
> But, the question is - if there is .next(), why there's no .first()?

When I'm getting elements from an iterator, I don't care whether
I'm getting the first element or a following one.  I just want the
next element.

Consider, for example, a function which wraps text into good-sized
lines, maybe hyphenating or what have you; this function accepts
as an argument an iterable containing lines of text.  I might call
this function directly on a file,
    wrap(fileobj)
(exploiting the fact that iterating over a file produces its
lines), or, say, read an RFC 822 header from the file first,
    it = iter(fileobj)
    for headerline in iter(it, ''):
        handleheader(headerline)
    wrap(it)
In the former case, the function sees the first element; in the
latter it sees elements after the first.  But why on earth should
it care?

-- 
Steven Taschuk                             staschuk at telusplanet.net
"I may be wrong but I'm positive."  -- _Friday_, Robert A. Heinlein





More information about the Python-list mailing list