How to create an object instance from a string??

Max M maxm at mxm.dk
Sun Mar 20 17:57:01 EST 2005


Tian wrote:
> How can I create an instance of an object from a string?
> 
> For example, I have a class Dog:


class Dog:
     def bark(self):
         print "Arf!!!"

def Factory(class_name):
     classes = {
         'Dog':Dog
     }
     return classes[class_name]


dog = Factory('Dog')()


-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Python-list mailing list