[Python-ideas] inspect.getclassdistance

Alexis Lee alexisl at hp.com
Mon Jan 5 15:43:39 CET 2015

Chris Angelico said on Mon, Jan 05, 2015 at 11:30:28PM +1100:
> def bfs_bases(cls):
>     """Yield the base which are belong to us"""
>     yield from cls.__bases__
>     for cls in cls.__bases__:
>         yield from bfs_bases(cls)

Much as I appreciate the pun, that isn't properly breadth-first, alas.
This is and returns the classes in tranches by distance in case you care
about the specific distance rather than a distance ordering:

    def bfs_bases2(cls):
        bases = [cls]
        while len(bases) > 0:
            yield bases
            bases = reduce(lambda x, y: x+y, (base.__bases__ for base in bases))

Nova Engineer, HP Cloud.  AKA lealexis, lxsli.

More information about the Python-ideas mailing list