vendredi 22 décembre 2017

Instantiate class dynamically with locate

I am trying to instantiate a class dynamically from a string that is fed to a method.

name = "folder.subfolder.classname"
my_class = locate(name)
instance = my_class()

Doing this results in an error:

TypeError: 'module' object is not callable

My question is: how can I dinamically know which class I need to call? If I hardcode the value and do my_class.classname() I can instantiate it, but I want that last part to be dynamic, instead of hardcoded. How can I do that?





Aucun commentaire:

Enregistrer un commentaire