I have a string like this:
classname = "Recipe"
Now I have a module “contenttype.py” and I want to do something like this:
import contenttype
myobject = contenttype.__getobject__(classname)(param=value)
Is there something similar to getattribute to be used in module’s top level?
I know I can do it with exec() but I am trying to avoid this.
Thanks
EDIT:
I found a way, but I am not convinced that it is good.
import inspect
myobject = dict(inspect.getmembers(contenttype))[classname](param=value)
A module is just an object, so yes, you can use
__getattribute__on it.But you don’t want to call
__getattribute__directly, that’s an implementation detail. For accessing named attributes on objects, including module members, usegetattr().