I have a module with ~100 classes and I want to get the class whose name matches a string I am supplying.
I used inspect to generate the list of the classes and names, and iterate through that, but is there another way to do it?
What I have:
def get_class_by_name(input_name_string): for (key, cls) in inspect.getmembers(my_module, inspect.isclass): if key == input_name_string: return cls
getattr(my_module, class_name)to retrieve whatever object has the name of the class you want? Which should be theclassobject..__subclasses__(). It would be helpful to give some context on what you're actually trying to achieve, though.vars(my_module)[class_name]would do as well.