Iterate over text file, discarding some lines via context manager

Marko Rauhamaa marko at pacujo.net
Fri Nov 28 12:55:40 EST 2014


Dave Angel <davea at davea.name>:

> Why would you prefer that over a generator function, as given earlier
> in the thread? See for example Ned's message. By using 'yield', you
> get Python to generate all the class boilerplate for you.

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.


Marko



More information about the Python-list mailing list