Iterators, iterables and special objects
> Works in what way? You can't use it in a 'for' loop if it doesn't
> define __iter__.
return <next item>
# No __iter__ here.
# I've just forgotten to def it.
With this setup, using for to iterate over Iterable *will* still work,
though you cannot use the trick described below.
> Yes? It is indeed an iterator, since its iter method returns itself. It
is also iterable, since it has an iter method.
Of course, you're right. But you're reading the article through the eyes of
an experienced developer.
> It's so you can do things like this:
> def show_with_header(stuff):
> stuff = iter(stuff)
> hdr = next(stuff)
> for line in stuff:
> if new_page(): print(hdr)
Yes, @Terry had given the same example. Frankly, I didn't know about it
before, I somehow had the misconception that for always got a 'fresh'