110

I have the need to take a string argument and create an object of the class named in that string in Python. In Java, I would use Class.forName().newInstance(). Is there an equivalent in Python?


Thanks for the responses. To answer those who want to know what I'm doing: I want to use a command line argument as the class name, and instantiate it. I'm actually programming in Jython and instantiating Java classes, hence the Java-ness of the question. getattr() works great. Thanks much.

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
Jason
  • 1,418
  • 2
  • 13
  • 10
  • See http://stackoverflow.com/a/10675081/116891 for an example of using `importlib.import`, which was backported from python 3 to 2.7 (https://docs.python.org/2/library/importlib.html) – Pat May 05 '15 at 00:47
  • I think all the complex gymnastics in the answers here directly contradict the assertion in the accepted answer that "reflection in python is a lot easier and far more flexible than it is in Java." I think a correct answer would be "no, it doesn't, so you have to write one yourself" (which is basically what @hasen's answer says). – Ken Williams Jun 03 '22 at 16:09

7 Answers7

188

Reflection in python is a lot easier and far more flexible than it is in Java.

I recommend reading this tutorial (on archive.org)

There's no direct function (that I know of) which takes a fully qualified class name and returns the class, however you have all the pieces needed to build that, and you can connect them together.

One bit of advice though: don't try to program in Java style when you're in python.

If you can explain what is it that you're trying to do, maybe we can help you find a more pythonic way of doing it.

Here's a function that does what you want:

def get_class( kls ):
    parts = kls.split('.')
    module = ".".join(parts[:-1])
    m = __import__( module )
    for comp in parts[1:]:
        m = getattr(m, comp)            
    return m

You can use the return value of this function as if it were the class itself.

Here's a usage example:

>>> D = get_class("datetime.datetime")
>>> D
<type 'datetime.datetime'>
>>> D.now()
datetime.datetime(2009, 1, 17, 2, 15, 58, 883000)
>>> a = D( 2010, 4, 22 )
>>> a
datetime.datetime(2010, 4, 22, 0, 0)
>>> 

How does that work?

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:

m = __import__( module )

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:

m = __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

This is what's happening in this loop:

for comp in parts[1:]:
    m = getattr(m, comp)    

At the end of the loop, m will be a reference to the class. This means that m is actually the class itslef, you can do for instance:

a = m() #instantiate a new instance of the class    
b = m( arg1, arg2 ) # pass arguments to the constructor

  
trs
  • 1,038
  • 9
  • 19
hasen
  • 161,647
  • 65
  • 194
  • 231
  • 7
    Exec is _extremely_ dangerous. It's easy to shoot yourself in the foot by overwriting names in your scope dynamically and just as easy to open a security flaw the size of Rhode Island. – Serafina Brocious Jan 17 '09 at 08:52
  • 1
    The reason exec is insecure here is that you can use a ';' in the class name to break out of the import. This can easily allow arbitrary code execution. The reason it can damage your code is that exec will overwrite colliding names, causing bugs and/or security flaws. – Serafina Brocious Jan 17 '09 at 09:01
  • 9
    Yep, this is what `__import__` exists for. Projects like Django that do module-loading-magic use it all the time. Nice answer. – cdleary Jan 17 '09 at 12:26
  • Yea, Django is what motivated me to learn about this magic (reflection) stuff. – hasen Jan 17 '09 at 12:46
  • 6
    `get_class = lambda name: reduce(getattr, name.split('.')[1:], __import__(name.partition('.')[0]))`. Though 'object.from_name' might be a better name. Examples: get_class('decimal.Decimal'), get_class(module_name). – jfs Jan 17 '09 at 14:16
  • I've tried this function but instead of a `type` I get a `classobj`. Any ideas why? and how can I actually get an instance of that class from it? – Mario Duarte Jul 12 '11 at 09:54
  • regarding the problem I stated in the comment above: I had to change the last line of `get_class` to `return m()`. – Mario Duarte Jul 21 '11 at 08:46
  • 3
    It only took to define a seven line function. A real ease. – Pavel Vlasov Apr 03 '12 at 10:11
  • 1
    See Pat's answer below for a way to do this with importlib which is cleaner. – sage88 Apr 21 '16 at 18:21
  • Link is dead, no longer up. – rjurney Jul 29 '19 at 05:49
30

Assuming the class is in your scope:

globals()['classname'](args, to, constructor)

Otherwise:

getattr(someModule, 'classname')(args, to, constructor)

Edit: Note, you can't give a name like 'foo.bar' to getattr. You'll need to split it by . and call getattr() on each piece left-to-right. This will handle that:

module, rest = 'foo.bar.baz'.split('.', 1)
fooBar = reduce(lambda a, b: getattr(a, b), rest.split('.'), globals()[module])
someVar = fooBar(args, to, constructor)
Serafina Brocious
  • 30,433
  • 12
  • 89
  • 114
  • Shouldn't it be globals()['classname']? – orip Jan 17 '09 at 09:09
  • 1
    You are indeed correct. I forgot globals() doesn't return the actual global scope, just a mapping of it. Editing as such -- thanks! – Serafina Brocious Jan 17 '09 at 09:21
  • This is not equivalent to java's Class.forName, in Java, no assumptions are made about what's imported and what's not – hasen Jan 17 '09 at 11:07
  • I mean, the only input is the fully qualified class name in string format, e.g. "org.eclipse.something.utils.date.DateTimeWidget", – hasen Jan 17 '09 at 11:09
  • 1
    `attrs = 'foo.bar.baz'.split('.'); fooBar = reduce(getattr, attrs[1:], __import__(attrs[0]))` – jfs Jan 17 '09 at 13:52
  • 1
    Or `module, _, attrs = 'foo.bar.baz'.partition('.'); fooBar = reduce(getattr, attrs, __import__(module))` – jfs Jan 17 '09 at 14:19
22
def import_class_from_string(path):
    from importlib import import_module
    module_path, _, class_name = path.rpartition('.')
    mod = import_module(module_path)
    klass = getattr(mod, class_name)
    return klass

Usage

In [59]: raise import_class_from_string('google.appengine.runtime.apiproxy_errors.DeadlineExceededError')()
---------------------------------------------------------------------------
DeadlineExceededError                     Traceback (most recent call last)
<ipython-input-59-b4e59d809b2f> in <module>()
----> 1 raise import_class_from_string('google.appengine.runtime.apiproxy_errors.DeadlineExceededError')()

DeadlineExceededError: 
Pat
  • 16,515
  • 15
  • 95
  • 114
  • 1
    +1 For using importlib to do this as it prevents the need (that __import__ causes) to recursively find the class. Simpler and cleaner than the accepted answer (though less well documented). – sage88 Apr 21 '16 at 18:19
4

Yet another implementation.

def import_class(class_string):
    """Returns class object specified by a string.

    Args:
        class_string: The string representing a class.

    Raises:
        ValueError if module part of the class is not specified.
    """
    module_name, _, class_name = class_string.rpartition('.')
    if module_name == '':
        raise ValueError('Class name must contain module part.')
    return getattr(
        __import__(module_name, globals(), locals(), [class_name], -1),
        class_name)
grayhemp
  • 381
  • 2
  • 4
  • The `if module_name` LBYL is pretty redudant, since the error you get if you simply delete those lines is: `ValueError: Empty module name`. – bukzor Feb 05 '14 at 01:05
3

It seems you're approaching this from the middle instead of the beginning. What are you really trying to do? Finding the class associated with a given string is a means to an end.

If you clarify your problem, which might require your own mental refactoring, a better solution may present itself.

For instance: Are you trying to load a saved object based on its type name and a set of parameters? Python spells this unpickling and you should look at the pickle module. And even though the unpickling process does exactly what you describe, you don't have to worry about how it works internally:

>>> class A(object):
...   def __init__(self, v):
...     self.v = v
...   def __reduce__(self):
...     return (self.__class__, (self.v,))
>>> a = A("example")
>>> import pickle
>>> b = pickle.loads(pickle.dumps(a))
>>> a.v, b.v
('example', 'example')
>>> a is b
False
2

This is found in the python standard library, as unittest.TestLoader.loadTestsFromName. Unfortunately the method goes on to do additional test-related activities, but this first ha looks re-usable. I've edited it to remove the test-related functionality:

def get_object(name):
    """Retrieve a python object, given its dotted.name."""
    parts = name.split('.')
    parts_copy = parts[:]
    while parts_copy:
        try:
            module = __import__('.'.join(parts_copy))
            break
        except ImportError:
            del parts_copy[-1]
            if not parts_copy: raise
    parts = parts[1:]

    obj = module
    for part in parts:
        parent, obj = obj, getattr(obj, part)

    return obj
bukzor
  • 37,539
  • 11
  • 77
  • 111
0

I needed to get objects for all existing classes in my_package. So I import all necessary classes into my_package's __init__.py.

So my directory structure is like this:

/my_package
    - __init__.py
    - module1.py
    - module2.py
    - module3.py

And my __init__.py looks like this:

from .module1 import ClassA
from .module2 import ClassB

Then I create a function like this:

def get_classes_from_module_name(module_name):
    return [_cls() for _, _cls in inspect.getmembers(__import__(module_name), inspect.isclass)]

Where module_name = 'my_package'

inspect doc: https://docs.python.org/3/library/inspect.html#inspect.getmembers