finding abc's

lars van gemerden lars at rational-it.com
Sat Jan 26 01:48:17 CET 2013


for future reference, i decided to go with 2 functions:

def common_bases(classes):
    if not len(classes):
        return None
    common = set(classes.pop().mro())
    for cls in classes:
        common.intersection_update(cls.mro()) #all subclasses in common       
    return [cls for cls in common if not any(sub in common for sub in cls.__subclasses__())] #the classes of which no subclasses are present

def unique_common_base(classes):
    while len(classes) > 1:
        classes = common_bases(classes)
    return classes.pop()

if i tested and understood correctly, they only take classes in the mro() into account (which might include abc's), the first gives all common base classes, the second recursively reduces further to one single class (the latter might not make to much sense, but in my program it is a safe bet for rare cases).

Thanks again for the help,

Cheers, Lars



More information about the Python-list mailing list