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?
Instead of storing class names in
classList, you could store the classes themselves:This way, instantiating the class whose name is stored in
xis as simple as