Method by name lookup

Fredrik Lundh fredrik at effbot.org
Thu Nov 2 14:39:10 EST 2000


Will Hartung wrote
> As an extra bonus, is there anyplace one can get all of the function names
> associated with an instance?

# builtin-dir-example-2.py
# from (the eff-bot guide to) The Python Standard Library
# http://www.pythonware.com/people/fredrik/librarybook.htm

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)

## prints:
## ['__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