-2

I have class A inside module consumers\a, B inside consumers\a and C inside consumers\c. I have used the following code to fetch the classes. Is there any other way to achieve the same. (this will work only if the class_name.lower() == module_name).

    bridge.py

    import inspect
    import types

    from consumers import a
    from consumers import b
    from consumers import c

    for name, val in globals().items():
    if isinstance(val, types.ModuleType):
        if 'consumers' in val.__name__:
            for k, v in inspect.getmembers(val):
                if k.lower() == name:
                    v()

I am trying this because I don't want the successor to do lot of work to add consumer.

I read, how-can-i-get-a-list-of-all-classes-within-current-module-in-python, /how-do-you-get-all-classes-defined-in-a-module-but-not-imported and few more links and came up with the above solution.

Community
  • 1
  • 1
John Prawyn
  • 1,423
  • 3
  • 19
  • 28
  • 2
    What are you trying to do? Is this a [XY problem](http://meta.stackexchange.com/a/66378)? –  Feb 05 '15 at 09:18

1 Answers1

0

Just import:

from consumers.a import A
from consumers.b import B
from consumers.C import C

a = A()
b = B()
c = C()
  • @JohnPrawyn with that many, I'd be inclined to put them in a list. Could you move the classes into a sub-module, and expose the list via `__init__.py`? – jonrsharpe Feb 05 '15 at 09:33