title: How to instantiate a class given its name as a string?
body: <p>Give some class definitiona in python, e.g.</p>
  
  <pre><code>class A(object):
      def __init__(self):
          self.x = 5
  
  class B(object):
      def __init(self):
          self.x = 42
  </code></pre>
  
  <p>I want to instantiate one of these classes given its name as string. So for example, if <code>classname</code> is <code>A</code>, then I want to do something like</p>
  
  <pre><code>myinstance = SOMETHING(classname)
  </code></pre>
  
  <p>which should correspond to </p>
  
  <pre><code>myinstance = A()
  </code></pre>
  
  <p>Is there a way to do this without the use of eval <code>eval</code> or maps? If not, I would do the following: </p>
  
  <pre><code>map = {'A': A, 'B': B}
  myinstance = map[classname]()
  </code></pre>