Subclass dynamically

Luis Alberto Zarrabeitia Gomez kyrie at
Sat Aug 8 21:13:04 CEST 2009

Quoting Robert Dailey <rcdailey at>:

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

1- Are you sure that you want that behavior? Given that in python, a class is
just a particular case of invocable object, you could just write a function
instead, and the code may be more robust.

2- If you are /sure/ that is the behavior you want, take a look at the __new__
method. The __new__ can decide which instance to return (you could even define a
new class inside of it, instantiate it, and return the instance).

3- You may want to take a look at metaclasses. But without more details about
why you want it, I can't give you more precise answers.


Luis Zarrabeitia
Facultad de Matemática y Computación, UH

Participe en Universidad 2010, del 8 al 12 de febrero de 2010
La Habana, Cuba

More information about the Python-list mailing list