dir() with string as argument

Steve Holden steve at holdenweb.com
Thu Jun 16 15:16:46 EDT 2005


Shankar Iyer (siyer at Princeton.EDU) wrote:
> Hi,
> 
> Suppose I have a string, sModuleName, that contains the name of a module.  I now want to see what functions are in that module, but if I call dir(sModuleName), I instead get the list of operations that can be done on a string.  Is there any way to convert the string into a format that I could feed to dir to cause the desired effect?  I think I could modify the string a bit and then use the exec command, but I was advised against that on this board last week.
> 
> Shankar
> 
The __import__() builtin function takes a module name and returns the 
imported module as a value.

  >>> m = __import__("bsddb")
  >>> m
<module 'bsddb' from '/usr/lib/python2.4/bsddb/__init__.pyc'>
  >>> dir(m)
['UserDict', '_DBWithCursor', '__builtins__', '__doc__', '__file__', 
'__name__', '__path__', '__version__', '_bsddb', '_checkflag', '_db', 
'_iter_mixin', '_openDBEnv', 'btopen', 'db', 'error', 'hashopen', 'os', 
'ref', 'rnopen', 'sys']
  >>>

regards
  Steve
-- 
Steve Holden        +1 703 861 4237  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/




More information about the Python-list mailing list