Phoenix
Phoenix

Reputation: 4656

Returning yield of lambda within iter

f=open('C:\\Python33\\text file.txt','r')

for c in iter(lambda: f.read(1),'\n'):
    print(?)

How would I print out the values which lambda: f.read(1) yields please?

Upvotes: 1

Views: 1399

Answers (1)

msvalkon
msvalkon

Reputation: 12077

Just print the c. That's what you're getting from iter() in your for loop.

f=open('C:\\Python33\\text file.txt','r')

for c in iter(lambda: f.read(1),'\n'):
    print(c)

Small improvement suggestion, use the with-statement:

with open('C:\\Python33\\text file.txt', 'r') as f:
    for c in iter(lambda: f.read(1), '\n'):
        print(c)

This way you won't have to call f.close().

Upvotes: 3

Related Questions