Python Lazy Iterator

I am trying to understand how and when iterator expressions are expressed. The following looks like a lazy expression:

g = (i for i in range(1000) if i % 3 == i % 2)

This one, however, is not executed when building:

g = (line.strip() for line in open('xxx', 'r') if len(line) > 10)

I do not have a file named "xxx". However, since this thing is lazy, why doesn't it work like that?

Thanks.

EDI: Wow, I did lazy!

g = (line.strip() for i in range(3) for line in open(str(i), 'r'))
+5
source share
2 answers

From the documentation :

, , next() ( , ). in for , , , .

+4

, open(), . open() .

+6

All Articles