Making sense of Stackless

Darrell darrell at dorb.com
Sun Feb 27 15:55:31 EST 2000


Thinking about how I could use Stackless and how it's different from using
callbacks on a object. I decided to view continuations as objects and here's
were it lead.

If an instance can be thought of as a stack frame then a continuation can be
thought of as an instance with a constructor and a run method.

import continuation

def construct():
    tripFlag=1
    c=continuation.caller()
    tf=c.update(tripFlag)
    if tripFlag==1:
            # Don't understand how tripFlag stops being equal to 1
        return c
    return tripFlag

def classFunc():
    print 'Construct:'
    x=1
    y=2
    c=construct()
    if c != None:
        return c
    else:
        print 'Run:'
        x=x+1
        print x+y

c=classFunc()
for x in range(3):
    c.call()

print '#'*20

def classIterator(seq):
    offset= -1
    c=construct()
    if c != None:
        return c
    else:
        offset=offset+1
        return seq[offset]

c=classIterator(range(10))
for x in range(3):
    print c.call()

####################
#Now Python has a class with truly private data.
#Unless Christian has a trick.

Construct:
Run:
4
Run:
5
Run:
6
####################
0
1
2


--Darrell






More information about the Python-list mailing list