Here’s a script-let that shall neatly print all user-defined classes (i.e. those made by you), as well as their base classes, in the global scope. This is extremely useful for creating object inheritance diagrams (such as the one in the mission “Hungry Games”). Simply use my_classes() anywhere in your code to run it.

Compatibility: Python 3.2+ (unconfirmed)

Tested with Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 (official build)


def my_classes():

    print("\nUser-defined classes in global scope:\n")

    # Reflect all clases in global scope
    from inspect import isbuiltin, isclass
    classes = []
    for x in dict(globals()).keys():
        # print(x)
        try:
            eval(x)
        except:
            print('Error in', x)
            continue
        if not isclass(eval(x)) or isbuiltin(eval(x)) or type(eval(x)) in [int, float, dict, str] or any([x in [int, float, str, dict] for x in eval(x).__bases__]):
            continue
        else:
            classes.append(eval(x))
    # print(classes)
    for name, bases in [(c.__name__, c.__bases__) for c in classes]:
        print("Class: %s" % name)
        print("Bases: %s" % (bases,))
        print()
    print('Total:', len(classes))