Laziness in Eager Languages
- Python
In Python 2.x the range
function computes a list of integers (eager or immediate evaluation):
In Python 3.x the range
function returns an iterator which computes elements of the list on demand (lazy or deferred evaluation):
- This change to lazy evaluation saves execution time for large ranges which may never be fully referenced and memory usage for large ranges where only one or a few elements are needed at any time.
Python manifests lazy evaluation by implementing iterators (lazy sequences) unlike tuple or list sequences. For instance:
>>> list = range(10) >>> iterator = iter(list) >>> print list >>> print iterator- The above example shows that lists are evaluated when called, but in case of iterator, the first element '0' is printed when need arises.
This section requires expansion. |
Read more about this topic: Lazy Evaluation
Famous quotes containing the words laziness, eager and/or languages:
“From passions grow opinions; intellectual laziness lets these harden into convictions.”
—Friedrich Nietzsche (18441900)
“In a time of war the nation is always of one mind, eager to hear something good of themselves and ill of the enemy. At this time the task of news-writers is easy, they have nothing to do but to tell that a battle is expected, and afterwards that a battle has been fought, in which we and our friends, whether conquering or conquered, did all, and our enemies did nothing.”
—Samuel Johnson (17091784)
“The very natural tendency to use terms derived from traditional grammar like verb, noun, adjective, passive voice, in describing languages outside of Indo-European is fraught with grave possibilities of misunderstanding.”
—Benjamin Lee Whorf (18971934)