Getting a list of ALL functions in a class

Eric Hopper eric.hopper at ebenx.com
Thu Mar 9 19:20:24 EST 2000


Eric Hopper wrote:

> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20000309/923d071f/attachment.html>


More information about the Python-list mailing list