Create object name from string value?
Tim Chase
python.list at tim.thechases.com
Thu Jan 21 10:32:52 EST 2010
Gnarlodious wrote:
>> for name in ["object1", "object2", "object3"]:
>> d = {name: classname()}
>> print d[name]
>
> This works! However I end up saying:
>
> d['Server'].Config.BaseURL
>
> to get the data, when I should be saying:
>
> Server.Config.BaseURL
It sounds like you want a mapping of strings to class objects
(not instances), so you can do something like
mapping = {
"object1": object1,
"object2": some_module.object2,
"object3": other_module.namespace.object3,
}
MyClass = mapping["object1"]
server = MyClass(initialization_params)
# the above two lines can be written as
# server = mapping["object1"](init_params)
print server.Config.BaseURL
-tkc
More information about the Python-list
mailing list