Iterate over text file, discarding some lines via context manager

Chris Angelico rosuav at gmail.com
Fri Nov 28 13:02:34 EST 2014


On Sat, Nov 29, 2014 at 4:55 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> I think the OP can learn from the comparison. One question, many
> lessons:
>
>  * Here's how you write a generator.
>
>  * Here's how you write a context manager class. You run into those
>    quite often as well.
>
>  * See how much more suitable a generator is in this case.
>
> No need to shoot down those who only try to be helpful.

Since we're so good at it on this list, I will nit-pick: that's not a
context manager class, that's an iterator class. A context manager has
__enter__ and __exit__, an iterator has __iter__ (returning self) and
__next__ (returning or raising StopIteration).

ChrisA



More information about the Python-list mailing list