Montag, März 26, 2007

"Open, reusable object models" in Python

It's roughly three or four weeks ago that I read an interview with Alan Kay ("Alan Kay: The PC Must Be Revamped—Now", CIO Insight, Feb 14, 2007). I found it quite inspiring, began surfing the Internet for more material and finally stumbled across Ian Piumarta's paper "Open, reusable object models" (2007/2/7) (see here also). I was fascinated by all the claims stated in the paper and started to have a closer look. Finally, in order to really understand what he did, I started coding his concept world in Python (my favourite programming language). The code is shown below. It's very close to the pseudo code used in the paper on page 5.

Before you have a look at the Python code, some remarks.

After the third rework of my code, I started renaming concepts. For my taste, e.g. "vtable" is not so intuitive. You will see that I introduce a "Object" class, a "ObjectBehavior" class and a "BehaviorBehavior" class in the code. Furthermore, I changed the names for the methods. They are now called "addClosure", "lookupClosure", "createObject", and "createBehavior". I think, the intentions of the object model get much clearer with the names changed.

When it comes to bootstrapping, I think that the code in Fig. 15 does not precisely re-construct the object model as shown in Fig. 5. When you look at the Python code, you'll see that the hard coded approximation and the bootstrapped model match quite nicely. This was also a main motivation to change names, as mentioned above. I have the feeling that it's now somewhat easier to see what is done in the paper and how the object model is "born".

lookupClosure = "lookupClosure"
addClosure = "addClosure"
createObject = "createObject"
createBehavior = "createBehavior"

class Closure(object):
def __init__(self,method,data=None):
assert callable(method)
self.method = method = data

def eval(self,obj,*args,**kwds):
assert isinstance(obj,Object)
return self.method(self,obj,*args,**kwds)

class Object(object):
def __init__(self,behavior,data=None):
assert isinstance(behavior,ObjectBehavior)
self.behavior = behavior = data

class ObjectBehavior(Object): # formerly 'VTable'
def __init__(self,behavior,delegate=None):
assert isinstance(behavior,BehaviorBehavior)
assert delegate == None or isinstance(delegate,ObjectBehavior)
self.delegate = delegate

class BehaviorBehavior(ObjectBehavior): # formerly 'VTableVTable'
def __init__(self):

def _addClosure(context,obj,key,closure): # see 'addMethod' (Fig. 10)[key] = closure

def _lookupClosure(context,obj,key): # see 'lookup' (Fig. 11)
if key in
if obj.delegate != None:
return send(obj.delegate,lookupClosure,key)
return None

def _createObject(context,obj,data=None): # see 'allocate' (Fig. 12)
return Object(obj,data)

def _createBehavior(context,obj,delegate=None): # see 'delegated' (Fig. 13)
return ObjectBehavior(obj,delegate)

addClosureClosure = Closure(_addClosure)
lookupClosureClosure = Closure(_lookupClosure)
createObjectClosure = Closure(_createObject)
createBehaviorClosure = Closure(_createBehavior)

def send(obj,messageName,*args,**kwds): # see 'function send()' p.5
assert isinstance(obj,Object)
closure = bind(obj,messageName)
return closure.eval(obj,*args,**kwds)

def bind(obj,messageName): # see 'function bind()' p.5
assert isinstance(obj,Object)
if obj.behavior == obj:
closure = lookupClosureClosure.eval(obj,messageName)
closure = send(obj.behavior,lookupClosure,messageName)
return closure

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Bootstrap the object universe (Fig. 15)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

_bb_ = BehaviorBehavior()

_b_ = send(_bb_,createBehavior,delegate=_bb_) # just an example
_o_ = send(_b_,createObject) # just an example

Any comments are welcome.

Keine Kommentare: