[Tutor] Workaround for limitation in xrange()?

Marc Poulin marc_a_poulin at yahoo.com
Tue Oct 10 21:31:06 CEST 2006


--- Dick Moores <rdm at rcblue.com> wrote:
> 
> Andrei's
> Write your own iterator:
>  >>> def hugerange(minval, maxval):
> ...     val = minval
> ...     while val < maxval:
> ...         yield val
> ...         val += 1
> 
> All 3 are essentially the same, aren't they. Which
> makes me feel even 
> dumber, because I don't understand any of them. I've
> consulted 3 
> books, and still don't understand the use of yield.
> 

This is an example of a "coroutine".
See http://en.wikipedia.org/wiki/Coroutine

def hugerange(minval, maxval):
    val = minval
    while val < maxval:
        yield val
        ### return value of "val" to calling routine
        ### at this point and place
        ### hugerange() function temporarily
        ### on hold ...
        ###
        ### next call to hugerange()
        ### resumes at this point with
        ### all previous variable values
        ### still intact ...
        val += 1


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the Tutor mailing list