title: Python create object depending on a variable name
body: <p>I am trying to create a Fruit object but I don't know exactly which fruit to create.</p>
  
  <p>I have a dictionary that loaded all the classes into a dictionary.</p>
  
  <pre><code>fruits = {'Apple': &lt;module 'Apple' from '.'&gt;, 'Orange': &lt;module 'Orange' from '.', 'Banana': &lt;module 'Banana' from '.'&gt;
  </code></pre>
  
  <p>I then have a variable that contains the fruit name I want to create. So I can set this to Apple, Orange, or Banana.</p>
  
  <pre><code>myfruit = 'Orange'
  </code></pre>
  
  <p>I also have some simple classes.</p>
  
  <pre><code>class Apple:
      def __init__(self):
          pass
  
  class Orange:
      def __init__(self):
          pass
  
  class Banana:
      def __init__(self):
          pass
  </code></pre>
  
  <p>I want to basically create a object based on whatever myfruit variable is set to.</p>
  
  <pre><code>fruit_obj = myfruit()
  </code></pre>
  
  <p>My end game is to be able to create a large amount of classes for various things, and then whenever myfruit is set, it creates a object of that type.</p>
  
  <p>EDIT:
  This is what I am doing to load the key (filename) to module.</p>
  
  <pre><code>def load_up(path):
      COMMAND_FOLDER = path + '/'
      commands = [f for f in listdir(COMMAND_FOLDER) if isfile(join(COMMAND_FOLDER, f)) and f[-3:] == '.py']
      commands_map = {}
      for command in commands:
          name = command[:-3]
          path = COMMAND_FOLDER + command
          spec = spec_from_file_location(name, path)
          foo = module_from_spec(spec)
          spec.loader.exec_module(foo)
  
          commands_map[name] = foo
  
      return commands_map
  </code></pre>