Java Reflection-Style Class Loading in Python
While working on Scoggle I wanted to be able to dynamically load matcher classes. This is very easy using Java with reflection. I found this SO article on how to do just that in Python. Given a package path to a class, this piece of code loads each module and returns the final class constructor:
def get_class( kls ):
= kls.split('.')
parts = ".".join(parts[:-1])
module = __import__( module )
m for comp in parts[1:]:
= getattr(m, comp)
m return m
The author explains how it works:
We’re using import to import the module that holds the class, which required that we first extract the module name from the fully qualified name. Then we import the module:
= __import__( module ) m
In this case, m will only refer to the top level module,
For example, if your class lives in foo.baz module, then m will be the module foo. We can easily obtain a reference to foo.baz using getattr( m, ‘baz’ ).
To get from the top level module to the class, have to recursively use gettatr on the parts of the class name
Say for example, if you class name is foo.baz.bar.Model then we do this:
= __import__( "foo.baz.bar" ) #m is package foo
m = getattr( m, "baz" ) #m is package baz
m = getattr( m, "bar" ) #m is module bar
m = getattr( m, "Model" ) #m is class Model m
This is what’s happening in this loop:
for comp in parts[1:]:
= getattr(m, comp) m
At the end of the loop, m will be a reference to the class. This means that m is actually the class itself, you can do for instance:
= m() #instantiate a new instance of the class
a = m( arg1, arg2 ) # pass arguments to the constructor b