Subclass dynamically

Piet van Oostrum piet at cs.uu.nl
Wed Aug 19 08:59:43 EDT 2009


>>>>> Robert Dailey <rcdailey at gmail.com> (RD) wrote:

>RD> Hey,
>RD> I have a class that I want to have a different base class depending on
>RD> a parameter that I pass to its __init__method. For example
>RD> (pseudocode):

>RD> class MyDerived( self.base ):
>RD>   def __init__( self, base ):
>RD>     self.base = base


>RD> Something like that... and then I would do this:

>RD> foo = MyDerived( MyBase() )

What do you want? As you write it now foo would be an instance of
MyDerived, but you say you want to have a class with a different base
class...

So does this mean that foo should become that class or that foo should
become an instance of a new anonymous class that has a specified base
class?

And on the other hand is MyBase the required base class. But you pass an
instance of MyBase, not MyBase itself. As you have it above MyBase()
should be a class, therefore MyBase should be a metaclass. Or is that
not what you want?
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list