[Tutor] Dynamic inheritance?

Kent Johnson kent37 at tds.net
Sat Nov 19 18:53:11 CET 2005


Jan Eden wrote:
> Hi,
> 
> I have a number of classes, each of which should inherit from a
> related superclass in another module:
> 
> class A(Super.A): ...
> 
> class B(Super.B): ...
> 
> class C(Super.C): ...
> 
> Is there a way to dynamically determine the value of Super at
> runtime? Background: Depending on certain object attributes which are
> set during the object initialization, I need to use a different set
> of templates for the respective object.

Sure, just import the correct template module conditionally and give it a new name:

if site == 'siteA':
  import SiteA as Super
else
  import SiteB as Super

class A(Super.A):

etc.

If you need to do the import based on a string use the __import__() function:
http://www.python.org/doc/current/lib/built-in-funcs.html

> The problem is that the template classes are not self contained, i.e.
> SiteB.Gallery inherits from SiteB.List, SiteB.Base etc.> 
> Thanks in advance,

I don't see why that is a problem?

Kent
-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list