Skip to content
Advertisement

Iterate the classes defined in a module imported dynamically

I have a module from a child package that is imported dynamically; how can I iterate over the classes that it contains?

I have been importing the module and listing the names like this:

JavaScript

This only prints module attributes and not the class types that the module defines:

JavaScript

It seems that my classes are not in the __dict__ unless the fromlist is non-empty! The values in the from-list don’t seem to be validated though; [""] seems to work just fine, and suddenly the classes show up!

Can anyone explain why this is?

(Standard ubuntu python 2.7.1+ (r271:86832)

Advertisement

Answer

Example: to create a dict that maps the names to the classes:

JavaScript

where mod is the loaded module

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement