Main Pages
Home
OpenGL
NumPy Tutorial
Email me
[email protected]

 

Acquisition

For a much more powerful version of acquisition in Python, look at the discussion Jim Fulton presents in his ExtensionClasses.

Acquisition is a version of inheritance based on an instance's environment (the instances it is a part of) rather than its genetics (its class hierarchy).

These are what I think are cool mixin classes which allow variants of a new OOP inheritance model which Gil and Lorenz have called Acquisition in a recent presentation. Python is not well suited to their definition of Acquisition (which emphasizes things like compile-time type safety), but I've come up with two versions which I think could come in quite handy.

If you don't know what acquisition is, here is the quick blurb I have in the docstring for Legacy.py:

    Basically, the problem is that while inheritance allows us not to have to repeat setting attributes for all of the instances in a is-a hierarchy (so that if class NiceCar is a subclass of class Car, then if Car has a method drive() then NiceCar will inherit it), there is no equivalent mechanism for 'has-a' relationships. This is most often noticed in systems like widget hierarchies. Chances are, if you have a Window which has 12 Widgets in it, and you set the background color of the Window to be 'blue', what you really mean is that all of the parts of Window should be 'blue' as well. By making both Widget and Window inherit from a class, then you can control which attributes get acquired. 
In other words, in both classes, the basic feature is that something like the following happens: 
        mixin = Legacy  # or Subscribing
        class Window(mixin):    
                def __init__(self):
                        self.color = 'blue'
                        self.button1 = Widget()
 
        class Widget(mixin):
                def describe(self):
                        print "Widget's color is", self.color
 
        >>> w = Window()
        >>> w.button1.describe()
        Widget's color is blue  
 
Note that a Widget is not a subclass of a Window, yet instances of it 'inherit' the color attribute. 

Note: These are mutually exclusive mixin classes. Use one or the other, but not both! Also, I don't think they'll work as is with other classes which override __getattr__, __setattr__ and __delattr__. 


Legacy.py [version 0.1]
Acquisition attribute lookup is performed when standard attribute lookup fails (in other words after local dictionary and inheritance tree lookup fail). [FUNCTION DEFINITIONS MAY NOT BE INHERITED -- NEED TO TEST]

 

 


Subscription.py [version 0.3]
Attribute setting propagates automatically at attribute setting time from containers to their parts, according to user-settable rules. Instances can override the subscribed values.

 

 

[Mon Jan 25 10:51:23 1999]