Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python get class from string

import importlib

def class_for_name(module_name, class_name):
    # load the module, will raise ImportError if module cannot be loaded
    m = importlib.import_module(module_name)
    # get the class, will raise AttributeError if class cannot be found
    c = getattr(m, class_name)
    return c
  
loaded_class = class_for_name('foo.bar', 'Baz')
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #python #class #string
ADD COMMENT
Topic
Name
3+2 =