FAQ
I am trying to get info about the classes in my running system. Having
gotten the modules, I wanted to get the classes defined in them. However,
classes = inspect.getmembers(module,inspect.isclass)
returns an empty list. This surprised me, so I inspected the __dict__ of the
module; the classes are not there, either (presumably that is where
getmembers() is getting its info). Am I missing something obvious? How
should I be getting the classes?

So that you understand why I'm asking, I am trying to implement a trace
(debugging purposes) that shows
call module.class.method(args...)
return module.class.method -> retvalue
for each method/function entered. Since I can only get the underlying
function (not class.method) in the sys.settrace() module, I am trying to
pre-parse my classes to find starting line numbers for methods, in order to
be able to correlate them with the functions I get in sys.settrace(). If
there is a much easier way to do this, I am open to suggestions, but please
answer the above question, also; it's got me bugged!

Search Discussions

  • Emile van Sebille at Aug 20, 2002 at 1:43 am
    Robert Dickinson
    I am trying to get info about the classes in my running system. Having
    gotten the modules, I wanted to get the classes defined in them. However,
    classes = inspect.getmembers(module,inspect.isclass)
    Hmmm.... this works for me:

    C:\>python
    ActivePython 2.1, build 210 ActiveState)
    based on Python 2.1 (#15, Apr 19 2001, 10:28:27) [MSC 32 bit (Intel)] on
    win32
    Type "help", "copyright", "credits" or "license" for more information.
    import urllib
    import inspect
    classes = inspect.getmembers(urllib,inspect.isclass)
    for ii in classes: ii
    ...
    ('FancyURLopener', <class urllib.FancyURLopener at 0080C0D4>)
    ('URLopener', <class urllib.URLopener at 00815894>)
    ('addbase', <class urllib.addbase at 00816DF4>)
    ('addclosehook', <class urllib.addclosehook at 00816E64>)
    ('addinfo', <class urllib.addinfo at 00818874>)
    ('addinfourl', <class urllib.addinfourl at 008188E4>)
    ('ftpwrapper', <class urllib.ftpwrapper at 007E500C>)
    >>>

    --

    Emile van Sebille
    emile at fenx.com

    ---------
  • Greg Ewing at Aug 20, 2002 at 4:51 am

    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
  • Robert Dickinson at Aug 20, 2002 at 4:11 pm
    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
  • Mike C. Fletcher at Aug 20, 2002 at 5:29 pm
    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
    yourself?

    Good luck,
    Mike


    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 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

    --
    _______________________________________
    Mike C. Fletcher
    Designer, VR Plumber, Coder
    http://members.rogers.com/mcfletch/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 19, '02 at 11:01p
activeAug 20, '02 at 5:29p
posts5
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase