Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python lazy iterator

I am trying to understand how and when iterator expressions get evaluated. The following seems to be a lazy expression:

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

This one, however fails on construction:

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

I do not have the file named 'xxx'. However, since this thing is lazy, why is it failing so soon?

Thanks.

EDI: Wow, I made a lazy one!

g = (line.strip() for i in range(3) for line in open(str(i), 'r'))
like image 373
Hamish Grubijan Avatar asked Feb 12 '10 04:02

Hamish Grubijan


People also ask

What is a lazy iterator Python?

Lazy iterators evaluate only when necessary. They allow us to semantically manipulate large amounts of data while keeping very little of it actually in memory. They act like lists but don't take up space.

What is lazy iterator?

In fact, many iterators calculate their values one at a time, as they are needed. Each time you request the next item, the iterator will calculate it there and then. This is called lazy evaluation – the iterator doesn't do any work until it absolutely has to.

What are the Iterables in Python?

Iterable is an object which can be looped over or iterated over with the help of a for loop. Objects like lists, tuples, sets, dictionaries, strings, etc. are called iterables. In short and simpler terms, iterable is anything that you can loop over.

What are the two types of iteration in Python?

There are two types of iteration: Definite iteration, in which the number of repetitions is specified explicitly in advance. Indefinite iteration, in which the code block executes until some condition is met.


2 Answers

From the documentation:

Variables used in the generator expression are evaluated lazily in a separate scope when the next() method is called for the generator object (in the same fashion as for normal generators). However, the in expression of the leftmost for clause is immediately evaluated in the current scope so that an error produced by it can be seen before any other possible error in the code that handles the generator expression.

like image 144
danben Avatar answered Oct 19 '22 02:10

danben


The iteration over the file returned by the call to open() is lazy. The call to open() is not.

like image 43
Ignacio Vazquez-Abrams Avatar answered Oct 19 '22 02:10

Ignacio Vazquez-Abrams