Getting a list of ALL functions in a class 
Author Message
 Getting a list of ALL functions in a class

I want to get a dictionary of all functions accessible in a class.
Doing <class>.__dict__ only gets me the functions defined in that class,
not any of it's base classes.

Is there a way to do this simply, or do I have to use __base__ to write
my own scanner?

I'm doing this because I have a set of thunking objects that are created
on the fly that make it look as if a set of methods on an instance are
actually a set of functions in the global namespace of a module.  I want
to do this because I'm setting up a sort of mini-syntax so people can
use python for quickly writing particular kinds of programs.

Thanks,
--Eric Hopper



Mon, 26 Aug 2002 03:00:00 GMT  
 Getting a list of ALL functions in a class

Quote:

> I want to get a dictionary of all functions accessible in a class.
> Doing <class>.__dict__ only gets me the functions defined in that class,
> not any of it's base classes.

> Is there a way to do this simply, or do I have to use __base__ to write
> my own scanner?

or you can cut and paste from this eff-bot guide
example:

# builtin-dir-example-2.py

class A:
    def a(self):
        pass
    def b(self):
        pass

class B(A):
    def c(self):
        pass
    def d(self):
        pass

def getmembers(klass, members=None):
    # get a list of all class members
    if members is None:
        members = []
    for k in klass.__bases__:
        getmembers(k, members)
    for m in dir(klass):
        if m not in members:
            members.append(m)
    return members

print getmembers(A)
print getmembers(B)
print getmembers(IOError)

## running this will print:
##
## ['__doc__', '__module__', 'a', 'b']
## ['__doc__', '__module__', 'a', 'b', 'c', 'd']
## ['__doc__', '__getitem__', '__init__', '__module__', '__str__']

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->



Mon, 26 Aug 2002 03:00:00 GMT  
 Getting a list of ALL functions in a class

Quote:

> I want to get a dictionary of all functions accessible in a class.
> Doing <class>.__dict__ only gets me the functions defined in that class,
> not any of it's base classes.

> Is there a way to do this simply, or do I have to use __base__ to write
> my own scanner?

I already know I can do the following:

-----
import types

def add_class_to_dict(cl, d, subadd):
   for basecl in cl.__bases__:
      subadd(basecl, d, subadd)
   for fn in dir(cl):
      if (type(cl.__dict__[fn]) == types.FunctionType):
         d[fn] = cl.__dict__[fn]

def class_func_dict(c, subadd=add_class_to_dict):
   d = {}
   subadd(c, d, subadd)
   return(d)

del add_class_to_dict
-----

I was just hoping there'd be a better way. It seems to me that the code is
making assumptions about how class name lookup works that may now be set in
stone, but I'd still rather not rely on.

It seems to me that if the code to do name lookups already exists that it
could be pressed into service to give you a complete dictionary of the
names available in a given class. If there isn't an interpreter hook for
this, then I guess I'll have to use the code above.

(Yeah, it's kind of a hack to remove the 'add_class_to_dict' name from the
module.  I wish Python had better namespace control sometimes.)

Thanks,
--Eric Hopper



Mon, 26 Aug 2002 03:00:00 GMT  
 Getting a list of ALL functions in a class

Quote:

> I want to get a dictionary of all functions accessible in a class.
> Doing <class>.__dict__ only gets me the functions defined in that class,
> not any of it's base classes.

> Is there a way to do this simply, or do I have to use __base__ to write
> my own scanner?

This might be what you're looking for:

http://tor.dhs.org/~zephyrfalcon/snippets/source/325.py

HTH,



Tue, 27 Aug 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Getting list of classes in a module?

2. getting the list of classes defined by a module

3. Getting a *full* list of attrib/methods for a class

4. Get a list of class (not instance) functions

5. class arguments: passing classes as function parameters

6. Strange positioning of class function calls within classes

7. Calling a derived class functions from base class

8. conflict class function name with ordinary function

9. Q: Tcl list commands and C++ List class

10. function while true - list function?

11. compare function in sort function of lists

12. TIP #15: Functions to List and Detail Math Functions

 

 
Powered by phpBB® Forum Software