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

A

Andy Leszczynski

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
 
M

Michele Simionato

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top