User Tools

Site Tools


cs_lang:python:things-to-know:lazy-vs-eager

Lazy vs Eager evaluation

Python generally uses an eager evaluation (in contrary to Haskell) but for example with Python 3 the range() function returns a special range object which computes elements of the list on demand.

>>> r = range(10)
>>> print(r)
range(0, 10)
>>> print(r[3])
3

This was not the case with Python 2 (without iterators):

>>> r = range(10)
>>> print r
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print r[3]
3

Lazy evaluation saves execution time for large ranges.

cs_lang/python/things-to-know/lazy-vs-eager.txt · Last modified: 2013/01/23 15:01 by cedric