Technically, what you're asking for (or at least the way everyone interprets it) is not a good practice, especially if you can take input from an unreliable source (remember that any source other than you should generally considered unreliable!). You must clearly state these things explicitly, because someone can initiate a function or create an object that you did not intend, with properties that you really do not need ...
Instead, you can do something like this (this, of course, is wildly incomplete, but should give you a general idea):
class Laptop(object): pass class Desktop(object): pass possible_classes = { "laptop": Laptop, "desktop": Desktop, } new_object = possible_classes[identifier_string](propA, propB, propC, ...)
Then just add a mapping for each new type of object in the dict_classes dict.
Nicholas knight
source share