finding abc's
lars van gemerden
lars at rational-it.com
Fri Jan 25 15:08:12 EST 2013
On Friday, January 25, 2013 8:08:18 PM UTC+1, Peter Otten wrote:
> 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
>
> relationship:
>
>
>
> >>> 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'>
>
> True
>
> >>> isinstance(1.1, Maybe)
>
> True
>
> >>> isinstance(1, Maybe)
>
> processing <class 'int'>
>
> False
>
> >>> issubclass(float, Maybe)
>
> True
>
>
>
> 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.
Thank you, interesting example. Added confirmation that trying to get the abc's is a bad idea.
More information about the Python-list
mailing list