Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lazy evaluation
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Python==== In [[Python (programming language)|Python]] 2.x the <code>range()</code> function<ref>{{cite web|url=https://docs.python.org/library/functions.html#range|title=2. Built-in Functions β Python 2.7.11 documentation}}</ref> computes a list of integers. The entire list is stored in memory when the first assignment statement is evaluated, so this is an example of eager or immediate evaluation: <syntaxhighlight lang="pycon"> >>> r = range(10) >>> print r [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> print r[3] 3 </syntaxhighlight> In Python 3.x the <code>range()</code> function<ref>{{cite web|url=https://docs.python.org/py3k/library/functions.html#range|title=2. Built-in Functions β Python 3.5.1 documentation}}</ref> returns a [[Generator (computer programming)|generator]] which computes elements of the list on demand. Elements are only generated when they are needed (e.g., when <code>print(r[3])</code> is evaluated in the following example), so this is an example of lazy or deferred evaluation: <syntaxhighlight lang="pycon"> >>> r = range(10) >>> print(r) range(0, 10) >>> print(r[3]) 3 </syntaxhighlight> :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. In Python 2.x is possible to use a function called <code>xrange()</code> which returns an object that generates the numbers in the range on demand. The advantage of <code>xrange</code> is that generated object will always take the same amount of memory. <syntaxhighlight lang="pycon"> >>> r = xrange(10) >>> print(r) xrange(10) >>> lst = [x for x in r] >>> print(lst) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] </syntaxhighlight> From version 2.2 forward, Python manifests lazy evaluation by implementing iterators (lazy sequences) unlike tuple or list sequences. For instance (Python 2): <syntaxhighlight lang="pycon"> >>> numbers = range(10) >>> iterator = iter(numbers) >>> print numbers [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> print iterator <listiterator object at 0xf7e8dd4c> >>> print iterator.next() 0 </syntaxhighlight> :The above example shows that lists are evaluated when called, but in case of iterator, the first element '0' is printed when need arises.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)