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:
“Six, or at most seven, hours sleep is, for a constancy, as much as you or anybody can want: more is only laziness and dozing, and is, I am persuaded, both unwholesome and stupefying.”
—Philip Dormer Stanhope, 4th Earl Chesterfield (16941773)
“There was no cornin the wide market-place
All loathliest things, even human flesh, was sold;
They weighed it in small scalesand many a face
Was fixt in eager horror then; his gold
The miser brought; the tender maid, grown bold
Through hunger, bared her scornèd charms in vain.”
—Percy Bysshe Shelley (17921822)
“No doubt, to a man of sense, travel offers advantages. As many languages as he has, as many friends, as many arts and trades, so many times is he a man. A foreign country is a point of comparison, wherefrom to judge his own.”
—Ralph Waldo Emerson (18031882)