finding abc's

Peter Otten __peter__ at
Fri Jan 25 20:08:18 CET 2013

lars van gemerden wrote:

> Hi all,
> i was writing a function to determine the common base class of a number
> classes:
> def common_base(classes):
>     if not len(classes):
>         return None
>     common = set(classes.pop().mro())
>     for cls in classes:
>         common.intersection_update(cls.mro())
>     while len(common) > 1:
>         cls1 = common.pop()
>         cls2 = common.pop()
>         if issubclass(cls1, cls2):
>             common.add(cls1)
>         elif issubclass(cls2, cls1):
>             common.add(cls2)
>     return common.pop()
> and ran common_base(int, float), hoping to get numbers.Number.
> this did not work because abstract base classes are not always in the
> mro() of classes.
> My question is: is there a way to obtain the abc's of a class or otherwise
> a way to make the function above take abc's into account (maybe via a
> predefined function)?

The abstract base classes may run arbitrary code to determine the subclass 
>>> from abc import ABCMeta
>>> import random
>>> class Maybe(metaclass=ABCMeta):
...     @classmethod
...     def __subclasshook__(cls, C):
...             print("processing", C)
...             return random.choice((False, True))
>>> isinstance(1.1, Maybe)
processing <class 'float'>
>>> isinstance(1.1, Maybe)
>>> isinstance(1, Maybe)
processing <class 'int'>
>>> issubclass(float, Maybe)

You'd have to check every pair of classes explicitly and might still miss 
(for example) numbers.Number as the module may not have been imported. 

I think you are out of luck.

More information about the Python-list mailing list