Dynamically Loading Classes in Python using Reflection/Inspection w/ Inspect Module
Python is a dynamic language that you can pretty much use for anything. One area that is pretty common is loading up a dynamic class. Let’s say you get back a list of classes and you want to initialize one into an object and use it. You can use the script below to load in a python class from introspection.
classname = "ClassToLookFor"
the_module = __import__("my.module", globals(), locals(), [classname])
the_class = getattr(the_module, classname)
obj = the_class()
More on introspection in Python (sometimes called Reflection in other languages like C# and Java):
Tags: introspection, python

