Getting a list of ALL functions in a class

Fredrik Lundh effbot at telia.com
Thu Mar 9 18:49:52 EST 2000


Eric Hopper <eric.hopper at ebenx.com> 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?

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





More information about the Python-list mailing list