Generator combined with with/using statement - Python vs C#

Posted on Fri 30 December 2011 in Coding • Tagged with c#, generator, Python

The other day, I tried to return a generator expression from within a with statement in Python, like so:

#!/usr/bin/python

def readlines(fn):
    with open(fn) as fd: # <-- the with statement
        return (line for line in fd) # <-- the generator expression

if __name__ == "__main__":
    for line in readlines("/proc …

Continue reading