class / module introspection?

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Wed Apr 2 15:33:43 EDT 2008


On 2 avr, 21:07, Brian Munroe <brian.e.mun... at gmail.com> wrote:
> On Apr 2, 11:04 am, "bruno.desthuilli... at gmail.com"
>
> <bruno.desthuilli... at gmail.com> wrote:
>
> > More seriously: the answer is in the doc.http://www.python.org/doc/2.3.5/lib/built-in-funcs.html
>
> > read about the __import__ function, experiment in your interactive
> > python shell, and you should be done in a couple minutes.
>
> Well, If I understand the docs correctly, that would work great if
> backends/ was a module and not a package?

Not necessarily.

>  I need to keep backends/
> system1/ and backends/system2 as separate directory structures to make
> things fairly isolated from each other (for neatness sake)
>
> Currently I'm building the backends/__init__.py __all__ list
> dynamically, such as:
>
> backends/__init__.py
> --------------------
>
> import os
>
> __all__ = []
>
> for module in os.listdir(__path__[0]):
>     if not module.startswith("__"):
>         __all__.append(module)

Why not do the import here, so you store a real module instead of a
name ?

ie (not tested):

for module_name in os.listdir(__path__[0]):
     if not module_name.startswith("__"):
         __all__.append(__import__(module_name, globals(), locals()))

My 2 cents...



More information about the Python-list mailing list