0

I was looking at the range function and an online search shows that (EDIT: in 2.x) it's eagerly evaluated

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

However when I try the code below in my local Python3.3 interpreter

a = range(10)
print(a)

I get an output

range(0, 10)

That's something I'd expect from a lazy evaluated function, what's the case ?

Note : In Python2.7 it always behaves as eagerly evaluated.

smci
  • 32,567
  • 20
  • 113
  • 146
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
  • 1
    @roippi Is it though ? How you'd expect someone who's primary language isn't Python to reach that other post ? I'm asking on terms ubiquitous across programming languages *eager* vs *lazy* **after** my Google search gave me just confusion on the topic – Nikos Athanasiou May 18 '14 at 18:21
  • 1
    Tagged [tag:python-2.x],[tag:python-3.x],[tag:range],[tag:lazy-evaluation]. Be aware that there may be version differences, so redo the keyword search with '2.x'/2.7/2.6 and '3.x' and see if it makes any difference. – smci May 18 '14 at 18:21
  • 3
    @NikosAthanasiou: you're right, but hang on. Closing-as-duplicate is not about chastizing the asker for (re-)asking what might be a perfectly relevant and important question (as in this case) but under a different set of terms/keywords/tags. Closing-as-duplicate is purely about keeping ***all equivalent questions on a single topic pointing to a single SO resource***. (See Meta.SO/.SE for endless debate...) – smci May 18 '14 at 18:24
  • 1
    @NikosAthanasiou That's the whole point; the whole reason your google search didn't work for you is that there aren't enough signposts linking (the terminology you searched for) with (the answer you were seeking). Duplicates serve as signposts connecting posts with their correct canonical answer. – roippi May 18 '14 at 18:27
  • See also [Is “backporting” Python 3's `range` to Python 2 a bad idea?](http://stackoverflow.com/questions/7507492/is-backporting-python-3s-range-to-python-2-a-bad-idea) – smci May 18 '14 at 18:28
  • A search on [lazy evaluation](http://en.wikipedia.org/wiki/Lazy_evaluation#Simulating_laziness_in_eager_languages) would also give you the answer. – devnull May 18 '14 at 18:52
  • Also see http://stackoverflow.com/questions/20535342/lazy-evaluation-python – devnull May 18 '14 at 18:57

2 Answers2

11

The behavior was changed between Python 2 and 3. In Python 2, it creates a list, so it is effectively eagerly evaluated. In Python 3, it creates a range object, whose individual values are lazily evaluated.

In Python 2, xrange provides a lazy version of range. In Python 3, if you want to force the whole lazy object to be evaluated into a real list, you can do list(range(10)).

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
2

As with all things, the situation in PyPy is weirder. In short, it's both.

Specifically, when a python2 program calls range(), the return value has type list, but its implementation is actually more like types.XRangeType; until you try to modify it, at which point it creates a "real" list.

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304