Search notes:

Python: Hierarchy of built-in types

This is the output of a python script that tries to print the class-hierarchy of Python built-in types
Note that OSError has three aliases (EnvironmentError, IOError and WindowsError) and that BuiltinImporter has one alias (__loader__).
BaseException
  Exception
    ArithmeticError
      FloatingPointError
      OverflowError
      ZeroDivisionError
    AssertionError
    AttributeError
    BufferError
    EOFError
    ImportError
      ModuleNotFoundError
    LookupError
      IndexError
      KeyError
    MemoryError
    NameError
      UnboundLocalError
    OSError (EnvironmentError, IOError, WindowsError)
      BlockingIOError
      ChildProcessError
      ConnectionError
        BrokenPipeError
        ConnectionAbortedError
        ConnectionRefusedError
        ConnectionResetError
      FileExistsError
      FileNotFoundError
      InterruptedError
      IsADirectoryError
      NotADirectoryError
      PermissionError
      ProcessLookupError
      TimeoutError
    ReferenceError
    RuntimeError
      NotImplementedError
      RecursionError
    StopAsyncIteration
    StopIteration
    SyntaxError
      IndentationError
        TabError
    SystemError
    TypeError
    ValueError
      UnicodeError
        UnicodeDecodeError
        UnicodeEncodeError
        UnicodeTranslateError
    Warning
      BytesWarning
      DeprecationWarning
      FutureWarning
      ImportWarning
      PendingDeprecationWarning
      ResourceWarning
      RuntimeWarning
      SyntaxWarning
      UnicodeWarning
      UserWarning
  GeneratorExit
  KeyboardInterrupt
  SystemExit
BuiltinImporter (__loader__)
builtin_function_or_method
bytearray
bytes
classmethod
complex
dict
enumerate
filter
float
frozenset
function
int
  bool
list
map
memoryview
module
property
range
reversed
set
slice
staticmethod
str
super
tuple
type
zip
This hierarchy was created with the following Python script:
builtin_types = {}

def ensureTypeInDict(t):
    global builtin_types

    if not id(t) in builtin_types:

       builtin_types[id(t)] = {
           'name'    :  t.__name__,
           'aliases' : [],          # special key to store names of aliases (for example: OSError is EnvironmentError is IOError is WindowsError)
           'children': []           # type ids of children
       }

for builtin_name in dir(__builtins__) + ['builtin_function_or_method', 'module', 'function']:

    if   builtin_name == 'builtin_function_or_method':
         builtin_obj = type(dir)

    elif builtin_name == 'module':
         builtin_obj = __builtins__.__class__

    elif builtin_name == 'function':
         builtin_obj = (lambda: None).__class__

    else:
         builtin_obj = getattr(__builtins__, builtin_name)

    builtin_type = builtin_obj

    if not issubclass(type(builtin_type), type):
       continue

    if builtin_type is object:
       continue

    assert len(builtin_type.__bases__) == 1

    type_name = builtin_type.__name__
    base_name = builtin_type.__bases__[0].__name__
    base_type = getattr(__builtins__, base_name)

    ensureTypeInDict(builtin_type)
    ensureTypeInDict(base_type   )

    if not id(builtin_type) in builtin_types[id(base_type)]['children']:
       builtin_types[id(base_type)]['children'].append(id(builtin_type))

    if builtin_name != type_name:
       builtin_types[id(builtin_type)]['aliases'].append(builtin_name)


def printTypeChildren(typeId, indent = -1):

    if indent >= 0:
       print('  ' * indent, end='')
       print(builtin_types[typeId]['name'], end='')

       if builtin_types[typeId]['aliases']:
          print(' ('                                       +
               ', '.join(builtin_types[typeId]['aliases']) +
               ')',
               end='')

       print('')

    for child_id in sorted(builtin_types[typeId]['children'], key=lambda λ: builtin_types[λ]['name'].upper()):
        printTypeChildren(child_id, indent+1)


printTypeChildren(id(object))
Github repository about-Python, path: /built-in/types/hierarchy.py
The script didn't report ellipsis, iterator, range_iterator, method, NoneType, traceback and possibly others.

TODO

function vs classmethod vs builtin_function_or_method

builtin in builtin_function_or_method does apparently not refer to built-in (default) Python, but that the function or method is written in C.
>>> def x(): pass
... 
>>> type(x)
<class 'function'>
>>> class CLS:
...    def meth(): pass
...    @staticmethod
...    def stat(): pass
... 

>>> type(CLS.meth)
<class 'function'>

>>> type(CLS.stat)
<class 'function'>

>>> obj = CLS()

>>> type(obj.meth)
<class 'method'>

>>> type(obj.stat)
<class 'function'>
>>> type(print)
<class 'builtin_function_or_method'>

Index