20
votes

So i have a set of classes and a string with one of the class names. How do I instantiate a class based on that string?

class foo:
  def __init__(self, left, right):
     self.left = left
     self.right = right

str = "foo"
x = Init(str, A, B)

I want x to be an instantiation of class foo.

6

6 Answers

19
votes

If you know the namespace involved, you can use it directly -- for example, if all classes are in module zap, the dictionary vars(zap) is that namespace; if they're all in the current module, globals() is probably the handiest way to get that dictionary.

If the classes are not all in the same namespace, then building an "artificial" namespace (a dedicated dict with class names as keys and class objects as values), as @Ignacio suggests, is probably the simplest approach.

26
votes

In your case you can use something like

get_class = lambda x: globals()[x]
c = get_class("foo")

And it's even easier no get class from the module

import somemodule
getattr(somemodule, "SomeClass")
6
votes
classdict = {'foo': foo}

x = classdict['foo'](A, B)
5
votes
classname = "Foo"
foo = vars()[classname](Bar, 0, 4)

Or perhaps

def mkinst(cls, *args, **kwargs):
    try:
        return globals()[cls](*args, **kwargs)
    except:
        raise NameError("Class %s is not defined" % cls)

x = mkinst("Foo", bar, 0, 4, disc="bust")
y = mkinst("Bar", foo, batman="robin")

Miscellaneous notes on the snippet:

*args and **kwargs are special parameters in Python, they mean «an array of non-keyword args» and «a dict of keyword args» accordingly.

PEP-8 (official Python style guide) recommends using cls for class variables.

vars() returns a dict of variables defined in the local scope.

globals() returns a dict of variables currently present in the environment outside of local scope.

2
votes

try this

cls = __import__('cls_name')

and this - http://effbot.org/zone/import-confusion.htm maybe helpful

0
votes

You might consider usage of metaclass as well:

Cls = type('foo', (), foo.__dict__)
x = Cls(A, B)

Yet it creates another similar class.