Create an instance of a class from a type class string like 'containers.Room'?
<p>I'm new to programming and I've chosen Python.  I've gone through all of learn python the hard way, I've coded my own 'choose your own adventure game' and now I'm moving on to creating something more akin to a MOO.  </p>

<p>I've structured my objects database to be very simple (everything in the game will be an object, and a separate attributes database will help customize things).  Current Object structure is like this:</p>

<p>objectid, name, typeclass, locationid</p>

<p>locationid is a foreign key back to the same objectid, that way objects can belong to a room, or a player.</p>

<p>My question comes with how I want to have typeclass set up.  Currently I want to store a string in there like: containers.Room.  When I pull the information from the database (or create a new room), I want to be able to make an instance of the class Room() contained in a separate python script called containers.  I know about being to do an from containers import room, but I don't know how to dynamically do it from a stored string.</p>

<p>Thank you in advance to anyone that may know and share the answer with me!</p>

<p><strong>edit for clarity</strong> (I rambled...)</p>

<p>I want to pull a string from my database ("module.Class"), import that module and class, and create an instance of said class.</p>

<p><strong>edit2</strong>
Using sqlite, here is my creation command.  At the end I want to return an instance of the typeclass.</p>

<pre><code>def create(self, **args):
    #requires name=string and typeclass=string.  no locationid for rooms
    #connect to the sqlite db and create a cursor for commands
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()


    #probably a better way to do this, but this is what I came up with
    columns = []
    values = []
    for k, v in args.iteritems():
        #iterate through arguments, if the column ends in 'id' assume int
        columns.append(k)
        if k[-2:] == "id":
            values.append("%s" % v)
        else:
            values.append(("'%s'" % v))

    #join columsn and rows and plug them into the query
    cols = ', '.join(columns)
    rows = ', '.join(values)

    query = "INSERT INTO objects_db (%s) VALUES (%s)" % (cols, rows)

    #execute the query, commit, and close the connection.
    cursor.execute(query)
    conn.commit()
    conn.close()

    &gt;&gt;&gt; stuff here to import args['typeclass'] &lt;&lt;&lt;
</code></pre>

<p>So I would be able to have a line like:</p>

<p>myobj = create(name="my object", typeclass="objects.things.Thing")</p>

<p>That would create a 'my object' in the database, and the 'myobj' variable would be an instance of the class Thing, found in the objects folder and things.py file.</p>

<p>So I figured out that my issue was due to trying to import modules from siblings when I was directly running my db.py script to test it.</p>

<p>I created a main.py file in my root directory for the project, allowing me to do something like this:</p>

<pre><code>    typeclass = args['typeclass'].split('.')
    import_path = "objects.%s" % typeclass[0]

    _temp = importlib.import_module(import_path)
    myobj = getattr(_temp, typeclass[1])

    return myobj(args['name'], args['locationid'])
</code></pre>

<p>The create function I'm working on is only used for objects, so I was able to assume an objects folder.  So I just needed the things.Thing part, so I was able to split it, import the script and get the attribute and run from there.</p>

python python-2.7