Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder5.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'elif': 0.04; 'explicitly': 0.04; 'classes.': 0.07; 'predefined': 0.07; 'imported.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'def': 0.10; '@classmethod': 0.16; 'c):': 0.16; 'classes:': 0.16; 'cls': 0.16; 'example)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'module': 0.19; 'all,': 0.21; 'import': 0.21; 'skip:_ 20': 0.22; 'random': 0.24; 'header:User-Agent:1': 0.26; 'common': 0.26; 'question': 0.27; 'header:X-Complaints-To:1': 0.28; 'run': 0.28; '(maybe': 0.29; 'van': 0.29; 'class': 0.29; 'classes': 0.30; 'function': 0.30; 'code': 0.31; 'to:addr:python-list': 0.33; 'false': 0.35; 'there': 0.35; 'received:org': 0.36; '(for': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'skip:n 10': 0.63; 'account': 0.67; 'hoping': 0.72; 'miss': 0.75; 'abc': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: finding abc's Date: Fri, 25 Jan 2013 20:08:18 +0100 Organization: None References: <766ec7eb-ab43-4c17-8073-3a0e6a8b89ea@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b64a.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359140888 news.xs4all.nl 6866 [2001:888:2000:d::a6]:54070 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37688 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 True >>> isinstance(1.1, Maybe) True >>> isinstance(1, Maybe) processing 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.