lundi 2 juillet 2018

How to instantiate class by it's string name in Python from CURRENT file?

Suppose I have myfile.py with some classes A, B and C defined INSIDE it. Now I want to instantiate class by it's name in str. I don't understand what to pass to getattr in order to do this. All examples like this assume that classes are in other module:

module = __import__(module_name)
class_ = getattr(module, class_name)
instance = class_()

but I don't have module_name.





Aucun commentaire:

Enregistrer un commentaire