dynamic creating of class

Salvatore artyprog at wanadoo.fr
Sat Jun 21 11:21:02 EDT 2003


Jaros?aw Zabie??o wrote:
> Is there a possibility for Python to dynamic binding of class?  I can
> do it in PHP but I do not know how to make the same in Python. 
> Look at the following example:

I had the same problem and I don't remember who gave me the solution.
The idea is to use 'eval'

class A:
     def __init__(self):
         self.class_name = 'A'

class B:
     def __init__(self):
         self.class_name = 'B'


choosed_class = 'A'
choosed_class += '()'
instance_of_choosed_class = eval(choosed_class)

Thanks to the one who gave me the solution :-)

Regards

Salvatore





More information about the Python-list mailing list