# delay and force in Python

Nick Coghlan ncoghlan at iinet.net.au
Wed Jan 19 14:53:28 CET 2005

```Will Stuyvesant wrote:
> The program below creates a stream with the numbers 1..995
> and then filters the stream, keeping only the even numbers,
> and then prints the second number in the stream (implemented
> as the first number of the tail, just like in the 3.5
> Section in the Wizard book).

How's this:

Py> from itertools import islice
Py> print islice((x for x in xrange(1, 996) if x % 2 == 0), 1, 2).next()
4

Breaking it into pieces:

Py> from itertools import islice
Py> stream = (x for x in xrange(1, 996) if x % 2 == 0)
Py> second_item = islice(stream, 1, 2).next()
Py> print second_item
4

And most of the stream hasn't been consumed yet:
Py> print stream.next()
6
Py> unconsumed = list(stream)
Py> len(unconsumed)
494

And this version has no problem with recursion limits:
Py> print islice((x for x in xrange(1, sys.maxint) if x % 2 == 0), 1, 2).next()
4

(xrange can't handle Python longs, unfortunately, so we *are* constrained by
sys.maxint. However, since my machine only has half a gig of RAM, the above is
still a damn sight quicker than the equivalent list comprehension would be!)

> Something else: this crashes with a "maximum recursion reached"
> . print stream_enumerate_interval(1,998)
>
> while this does not crash
> . print stream_enumerate_interval(1,900)
> this means Python has a maximum of something like 900
> recursions?

The CPython implementation is limited by the stack size allocated by the C
runtime library. The exact recursion limit is platform dependent, but something
around 1000 sounds fairly normal.

Cheers,
Nick.

--
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net

```