Just-In-Time importing

echuck at mindspring.com echuck at mindspring.com
Thu Jun 15 09:43:25 EDT 2000


In article <Pine.BSF.4.10.10006132236080.23561-100000 at chi.pair.com>,
  Michal Wallace <sabren at manifestation.com> wrote:

> Now, if zikebase.Content were usually a subclass of zikebase.Node (it
> isn't), creating a zikebase.Content() would produce something that was
> a subclass of Vertex instead. This can come in really handy if you
> want to add something to every class in an object hierarchy and don't
> want to (or can't) actually change the original top-level code. (It
> sure beats subclassing every item in the tree, or writing a
> wrapper-factory)
>
> Cheers,
>
> - Michal
>

Michal, you can also accomplish that by adding the base class at run
time. I use this:

def MixIn(klass, newBase):
    if newBase not in klass.__bases__:
        klass.__bases___ = klass.__bases__ + (newBase,)


Now I can say:

MixIn(ModelObject, ModelObjectInterface)


to add rendering methods to ModelObject in my web and/or gui app.

-Chuck


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list