Changing ancestor classes at runtime 
Author Message
 Changing ancestor classes at runtime

Hi,

If I have classes, A, B and C, where C inherits from B like so:

class A:
    pass

class C(B):
    pass

Is there a way, at runtime, to make C also inherit from A.  The key here is _at runtime_.

I didn't see anything obvious in dir(SomeClass) that lets me do something like class.__supers__.extend(otherClass).

-Chuck



Thu, 05 Sep 2002 03:00:00 GMT  
 Changing ancestor classes at runtime
[Chuck Esterbrook]

Quote:
> Is there a way, at runtime, to make C also inherit from A.  The key here
is _at runtime_.

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

class B:
    def b(self):
        print self.__class__

class Top:
    pass

t=Top()
l=list(Top.__bases__)
l.append(B)
l.append(A)
Top.__bases__=tuple(l)

t.b()
t.a()

### Output

__main__.Top
__main__.Top

--Darrell



Thu, 05 Sep 2002 03:00:00 GMT  
 Changing ancestor classes at runtime

Thanks, this technique worked for me. The code I ended up using is this:

if not newBaseClass in aClass.__bases__:
        aClass.__bases__ = aClass.__bases__ + (newBaseClass,)

-Chuck

Quote:

> [Chuck Esterbrook]
> > Is there a way, at runtime, to make C also inherit from A.  The key here
> is _at runtime_.

> class A:
>     def a(self):
>         print self.__class__

> class B:
>     def b(self):
>         print self.__class__

> class Top:
>     pass

> t=Top()
> l=list(Top.__bases__)
> l.append(B)
> l.append(A)
> Top.__bases__=tuple(l)

> t.b()
> t.a()

> ### Output

> __main__.Top
> __main__.Top

> --Darrell



Thu, 05 Sep 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Changing list view at runtime?

2. Change superclass at runtime

3. changing menues at runtime in VW ?

4. changing menues at runtime in VW ?

5. change control tab order at runtime

6. changing file directory at runtime without using a variable in prop:name

7. Changing file attributes at runtime

8. Changing list box columns at runtime

9. Changing input field picture format at runtime?

10. Changing Picture string at runtime

11. Changing Required attribute at runtime

12. Change File Driver at Runtime

 

 
Powered by phpBB® Forum Software