dynamic bases

Balazs Scheidler bazsi at balabit.hu
Fri Jul 21 11:16:35 EDT 2000


Hi,

I'm new to this group, and new to Python itself. I'm using Python as a
configuration language in one of my homebrewn project. And until now Python
served me perfectly. I'm confident that my current problem is 100% solvable,
but I don't want ugly solutions, so I decided to turn to more experienced
people.

So what I basically want is to add a set of methods/attributes to an
instance dynamically, so that the methods added can access the attributes of
the original instance. My original idea was the following:

class A:
        def F(self):
                print self.a

class B:
        a=5
        def __init__(self, klass):
                self.__class__.__bases__ = self.__class__.__bases__ + (klass,)

b = B(A)
b.F()

The problem with this approach is that it modifies the class-object of B, so
that every other instance of B gets klass as its base object. I'd like to
do this on a per instance basis.

My other attempt was to use __getattr__ hooks to return attributes in a way
that attributes of A are returned in case __dict__ of B doesn't contain a
given name. Functions were bound to the instance of B, then the Python
interpreter complained that self is not a subclass of B.

I was reading about meta-classes, and even Python Extension Classes used by
Zope, maybe these could offer a clean solution?

-- 
Bazsi
PGP info: KeyID 9AF8D0A9 Fingerprint CD27 CFB0 802C 0944 9CFD 804E C82C 8EB1
     url: http://www.balabit.hu/pgpkey.txt




More information about the Python-list mailing list