Classes are not members of their respective modules??

Robert Dickinson rdickins at usaor.net
Tue Aug 20 18:11:46 CEST 2002


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 module.name 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
> http://www.cosc.canterbury.ac.nz/~greg
>





More information about the Python-list mailing list