how to find available classes in a file ?

Alex Popescu the.mindstorm.mailinglist at gmail.com
Wed Jul 18 06:00:01 EDT 2007


On Jul 17, 4:41 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Mon, 16 Jul 2007 20:13:19 -0300, Alex Popescu  
> <the.mindstorm.mailingl... at gmail.com> escribió:
>
> > On Jul 17, 1:44 am, Stef Mientki <S.Mientki-nos... at mailbox.kun.nl>
> > wrote:
> >> I want to have a (dynamically) list of all classes defined in a py-file.
> >> Is there a way of getting this list, without manually parsing the file ?
>
> > I have written something that does something like this, but I am not
> > sure it is the pythonic way.
> > Bascially I am loading the module and then using dir() on the module
> > object I am looking for
> > attribute of the type classobj (for old style classes) and type type
> > (for new style classes).
>
> There is also the pyclbr standard module  
> <http://docs.python.org/lib/module-pyclbr.html> that does not load the  
> module (just reparses enough of it to get the info needed). So a broken  
> import, or any other initialization error, won't hurt.
>
> > I also heard of the inspect module, but I haven't checked it yet.
>
> inspect.getmembers(the_module, inspect.isclass) would do. But this  
> requires the module to be importable.
>
> --
> Gabriel Genellina

I think I have found a difference between my dir(module) based
implementation
and the inspect.getmember(module, inspect.isclass) functionality. Mine
is
returning only the classes defined in the provided module, while the
later is
returning also the imported classes. Am I getting the results
wrongly?
(I don't seem to see this explanation in the inspect.getmembers doc or
I am just
misreading it).

./alex
--
.w( the_mindstorm )p.





More information about the Python-list mailing list