Exposing all methods of a class

Steve D'Aprano steve+python at pearwood.info
Wed Mar 1 09:38:14 EST 2017

On Mon, 27 Feb 2017 07:15 am, Pete Dowdell wrote:

> I use Python, mainly with Django, for work. I was wondering if anyone
> has encountered an editor that could display a class with all inherited
> methods included in the editor's view of the class code.

Python 3.5 (if not earlier) can do most of the heavy lifting for this.
Here's a proof of concept:

import inspect

def expose(obj):
    if isinstance(obj, type):
        cls = obj
        cls = type(obj)
    attrs = inspect.classify_class_attrs(cls)
    by_class = {}
    for attr in attrs:
        if attr.name.startswith('_'):
        by_class.setdefault(attr.defining_class, []).append(attr)
    print("%s object %r" % (type(obj), obj))
    for K in by_class:
        print("Inheriting from: %s" % K)
        for attr in by_class[K]:
            print("    ", attr.name, attr.kind)

“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

More information about the Python-list mailing list