Classes are not members of their respective modules??

Mike C. Fletcher mcfletch at
Tue Aug 20 19:29:42 CEST 2002

There are tricks modules can play, depending on what module you're 
using, it may not actually be what you think it is.  Some modules use 
lazy importing to load heavy class defn's only when needed, others may 
define classes using functions which only create the class object when a 
method or function is called, yet others may use objects which are very 
class-like, but which are not in fact classes (though that doesn't seem 
to be the problem here).  Alternately, are you maybe passing the wrong 
module (for instance, an old version with just the functions)?

assert module.className == classObject, "You can't get to it normally, 
possibly using the wrong module object"

assert type(module) == types.ModuleType, "It's not a regular module 
object, it's a stand-in doing weird things"

BTW, would help in debugging if we knew what the module is (e.g. is it 
one of the mx.* or wx.* modules, or a standard library module, a 
third-party module for which you have source, or a module you created 

Good luck,

Robert Dickinson wrote:
> Thanks for confirming my astonishment. I was sure they should be there too.
> Maybe I'm making some egregious error in my code. This executes in my
> sys.settrace() function, on a "call"-type call to that function. The module
> must be fully initialized because I'm calling a user method in a class in
> that module.
>                     functions =
> inspect.getmembers(module,inspect.isfunction)
>                     print "functions: %s"%functions
>                     classes = inspect.getmembers(module,inspect.isclass)
>                     if classes==[]:
>                         moduleDict = module.__dict__
>                         for mkey in moduleDict.keys():
>                             if inspect.isclass(moduleDict[mkey]):
>                                 classes.append((mkey,moduleDict[mkey]))
>                     print "classes: %s"%classes
> always prints:
>     functions: [....]    #lists functions properly
>     classes: []    #always empty
> I thought that perhaps the inspect.isclass function didn't do what I thought
> it should, so I tried just printing the moduleDict (in place of the "for
> mkey.." loop above) and, sure enough, the class names are not in there.  :-O
> I'm willing to accept that I've made a stupid mistake, but I sure can't find
> it. Any suggestions?
> Greg Ewing wrote:
>>Robert Dickinson wrote:
>>>I inspected the __dict__ of the
>>>module; the classes are not there, either
>>That's strange, they certainly should be there (every name
>>accessible through should be there, in fact).
>>Have you somehow got hold of an incompletely-constructed
>>module? That can happen if importation of a module fails
>>part way through.
>>Greg Ewing, Computer Science Dept,
>>University of Canterbury,
>>Christchurch, New Zealand

   Mike C. Fletcher
   Designer, VR Plumber, Coder

More information about the Python-list mailing list