Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I don't understand what's happening when the following function is called:

def doSmth(inFile):
    print inFile
    with open(inFile,'r') as trainSet:
        for instLine in trainSet:

            # do smth
            yield instLine

why when the last line with yield is removed there's an error that the file doesn't exist (it really doesn't exist). Whereas, when I have the line , there's no error. Another question, why in the second case, print inFile has no effect while it has in the first case. Thanks.

share|improve this question
up vote 3 down vote accepted

Because you aren't actually iterating over the generator:

You have something similar to this in your code:

doSmth() # simply creates generator without advancing it

without the for x in doSmth()

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.