Getting a list of classes in the current module/auto introspection

Discussion in 'Python' started by Andy Leszczynski, Apr 28, 2005.

  1. Hi,
    I have a following problem. Let's say there is a module which could be
    imported or run as __main__. It is going to contain hundreds of classes,
    something like that:

    import moduleA
    from moduleB import *

    class A:
    pass

    class B:
    pass

    class C:
    pass

    [...]


    I would like to be able to get references (or names) of all of them but
    excluding stuff from moduleA and module B. Is there any pythonic/elegant
    way to do that?

    Thanks, Andy
    Andy Leszczynski, Apr 28, 2005
    #1
    1. Advertising

  2. Any class has a .__module__ attribute giving the name
    of the module where the class was defined, so you
    need something like

    [c for c in globals() if inspect.isclass(c) and c.__module__ ==
    "__main__"]

    Michele Simionato
    Michele Simionato, Apr 28, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy Smith

    Introspection at the module level?

    Roy Smith, Mar 6, 2004, in forum: Python
    Replies:
    6
    Views:
    283
    Roy Smith
    Mar 7, 2004
  2. Benjamin Rutt
    Replies:
    4
    Views:
    382
    Mike Meyer
    Jul 8, 2005
  3. R. Bernstein
    Replies:
    8
    Views:
    389
    R. Bernstein
    Jan 16, 2006
  4. Kirk McDonald

    Introspection with classes

    Kirk McDonald, Jan 31, 2006, in forum: Python
    Replies:
    0
    Views:
    239
    Kirk McDonald
    Jan 31, 2006
  5. linkswanted
    Replies:
    1
    Views:
    887
Loading...

Share This Page