19

I've got a module written in Python. I now want to import it into another script and list all classes I defined in this module. So I try:

>>> import my_module
>>> dir(my_module)
['BooleanField', 'CharField', 'DateTimeField', 'DecimalField', 'MyClass', 'MySecondClass', 'ForeignKeyField', 'HStoreField', 'IntegerField', 'JSONField', 'TextField', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'datetime', 'db', 'division', 'os', 'struct', 'uuid']

The only two classes which I defined in my_module are MyClass and MySecondClass, the other stuff are all things that I imported into my_module.

I now want to somehow be able to get a list of all classes which are defined in my_module without getting all the other stuff. Is there a way to do this in Python?

2
  • 2
    While it is possible to get this information, trying to do so is fragile and results in unobvious behaviour. In general, reflection is very useful for debugging, but should be considered very carefully before being used in actual production code. Commented Mar 22, 2014 at 14:13
  • To document which names may be used outside (public), you could define __all__ attribute: [cls for name in your_module.__all__ for cls in [getattr(your_module, name)] if inspect.isclass(cls)] it works even if the class is defined in a submodule but you want it to be available from toplevel module. Try it with asyncio module, to see what I mean Commented Mar 24, 2014 at 6:58

3 Answers 3

51

Use the inspect module to inspect live objects:

>>> import inspect
>>> import my_module
>>> [m[0] for m in inspect.getmembers(my_module, inspect.isclass) if m[1].__module__ == 'my_module']

That should then work, getting every class defined within that my_module.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice. Probably if m[1].__module__ == my_module.__name__ would be nicer.
8

You can use Python Class Browser

import pyclbr
module_name = 'your_module'
module_info = pyclbr.readmodule(module_name)
print(module_info)

for item in module_info.values():
    print(item.name)

it will list all classes defined in your_module

2 Comments

Nice, is this module installed with Python by default or does it require to be installed manually?
@Alexis.Rolland it's from standard library
1

If you really want to use dir(), here it is:

>>> import module
>>> [eval("module." + objname) for objname in dir(module) if type(eval("module." + objname)) is type]

or (script form)

import module
classes = []
for objname in dir(module):
    obj = eval("module." + objname)
    if type(obj) is type:
        classes.append(obj)
print(classes)

But this uses the "eval" function, and is really not safe for use. I'd stick to the chosen answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.