In response to your update: the only way to do this is through a matching system. If you do not want to use an explicit mapping system, then you can use one of the built-in Python mapping systems, although, in my opinion, it is less clear and clear.
obj = globals()[parser['name']]()
will access a global object named parser['name'] == 'foo' . If this is a class (or the class that you really want to create based on user input), then you should be good to go. Otherwise, you will have to build logic around it in order to whitelist those classes that you really want.
If classes come from a module, you can use this __dict__ attribute for the same effect.
obj = somemodule.__dict__[parser['name']]()
The same warnings apply to this situation as the previous one. Better to just use explicit mapping
aaronasterling
source share