Subclass dynamically

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


Quoting Robert Dailey <rcdailey at gmail.com>:

> 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.

Regards,

-- 
Luis Zarrabeitia
Facultad de Matemática y Computación, UH
http://profesores.matcom.uh.cu/~kyrie



-- 
Participe en Universidad 2010, del 8 al 12 de febrero de 2010
La Habana, Cuba 
http://www.universidad2010.cu




More information about the Python-list mailing list