__import__ with dict values
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Thu Mar 12 14:00:40 EDT 2009
En Thu, 12 Mar 2009 09:27:35 -0200, alex goretoy
<aleksandr.goretoy at gmail.com> escribió:
>> note i would still like to be able to do __import__("sys")."path"
p = __import__("sys").path
That's a convoluted way of doing:
import sys
p = sys.path
(except that the latter one inserts "sys" in the current namespace)
>> maybe if __import__ had __str__ defined, How is my thinking on this?
>> and how would I achieve something like this?
__str__ has absolutely nothing to do.
> __import__(opt['imp_mod']).options
>
> eval(opt['imp_mod']+"."+opt['imp_opt'])
>
> how to make top work like bottom?
If you think you have to use eval: you don't. Never.
module = __import__(opt['imp_mod'])
module.options
If the name "options" is not known until runtime, use getattr:
getattr(module, name_of_attribute)
The above assumes you want an attribute (like logging.ERROR). If you want
a sub-module (a module inside a package) use __import__("dotted.name") and
then retrieve the module by name from sys.modules; see
http://docs.python.org/library/functions.html#__import__
--
Gabriel Genellina
More information about the Python-list
mailing list