Take the 2-minute tour ×

I'm working on a python project, where there are a bunch of classes created already. I will be asking the user to enter which class to be executed (i.e., the name of the class, whose list will be shown to him).

After he chooses a choice, I would like to create an instance of the class requested by the user and show the appropriate output.

The scenario is like this...

My Class definitions go up here...

classList = {'a':'AClass','b':'BClass','c':'CClass'}
for i in iter(classList):
   print i
x = raw_input('Enter class name: ') 'Get the user input and store it.'
I'm stuck here.
Have to create an instance for the requested class.
i.e., have to create an instance to AClass if the user entered a (classList[a])

A Procedure, I know, but vague is to use switch case or if-else to check that input and create instance for the corresponding class. But, I'm new to python, so I want to know if there is any other way of achieving this in a more elegant manner?

share|improve this question
For the record, """ .... """ is not a python "block comment" (though I've seen it used that way many times). It creates a string to just be thrown away and it can lead to indentation errors. –  mgilson Nov 8 '12 at 15:51

3 Answers 3

up vote 2 down vote accepted

Instead of storing class names in classList, you could store the classes themselves:

class AClass(object): pass
class BClass(object): pass
class CClass(object): pass

classList = {'a':AClass, 'b':BClass, 'c':CClass}

This way, instantiating the class whose name is stored in x is as simple as

obj = classList[x]()
share|improve this answer
Can you please elaborate on this.. I mean storing classes, I don't know that –  Rajesh Nov 8 '12 at 15:53
In Python, classes themselves are run-time objects. You can store a reference to a class just like you would store a reference to any other object. The code in my answer illustrates this. –  NPE Nov 8 '12 at 15:54
Thank you very much.. It helped me. –  Rajesh Nov 8 '12 at 15:58

If you still want to create an class instance from a string then here are my vanilla functions for that:

def get_object_type(classname, modulename):
    return getattr(sys.modules[modulename], classname)

def get_object_instance(classname, modulename, param={}):
    return get_object_type(classname, modulename)(**param)

new_instance = get_object_instance("ClassA", "modulename")
share|improve this answer

You're off to a decent start, but you need to remember that you can pass classes around. So this would work:

class A(): pass

class B(): pass

map = {'a': A, 'b': B}

instance = map['a']()
share|improve this answer
I wouldn't use map as a variable name -- you shadow the builtin that way. Maybe class_mapping instead? –  mgilson Nov 8 '12 at 15:49
@mgilson the map function should be dead BTW... –  JBernardo Nov 8 '12 at 15:50
@JBernardo -- That might be your opinion, but I've seen many SO answers which use it and get upvoted, so apparently not everyone agrees with that sentiment. It's also useful to know because of things like multiprocessing.Pool.map which behave roughly the same way. map will never be dead until Guido decides to execute it ... and I don't think he's gonna kill it anytime soon, so we might as well treat it with similar respect that we give other builtins (e.g. list) which you could argue is also unnecessary -- it's the same as list(obj) == [x for x in obj] –  mgilson Nov 8 '12 at 15:55
@mgilson there's this famous post from GvR saying it will be removed from Py3k and, after many complaints, he decides to leave it but now returning an iterator instead of a list... It breaks the "should be one-- and preferably only one --obvious way to do it" –  JBernardo Nov 8 '12 at 15:59

Your Answer


