Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'elif': 0.04; 'explicitly': 0.04; 'classes.': 0.07; 'predefined': 0.07; 'friday,': 0.09; 'imported.': 0.09; 'subclass': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; '@classmethod': 0.16; 'c):': 0.16; 'classes:': 0.16; 'cls': 0.16; 'example)': 0.16; 'wrote:': 0.17; 'example.': 0.17; '>>>': 0.18; 'module': 0.19; 'all,': 0.21; 'trying': 0.21; 'import': 0.21; 'skip:_ 20': 0.22; 'cc:2**0': 0.23; 'random': 0.24; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'common': 0.26; 'question': 0.27; 'run': 0.28; '(maybe': 0.29; 'van': 0.29; 'class': 0.29; 'classes': 0.30; 'function': 0.30; 'code': 0.31; 'received:google.com': 0.34; 'false': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'thank': 0.36; 'bad': 0.37; '(for': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'think': 0.40; 'skip:n 10': 0.63; 'account': 0.67; 'hoping': 0.72; 'miss': 0.75; '2013': 0.84; 'otten': 0.84; 'abc': 0.91 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:path:newsgroups:date:in-reply-to:complaints-to :injection-info:nntp-posting-host:references:user-agent :x-google-web-client:x-google-ip:mime-version:message-id:subject :from:to:cc:content-type:x-gm-message-state; bh=ygJnz3cVJNKJfq5OR9xq5l9uluRhYeX2mrc6NKELLUU=; b=Z/k+8T9NJrl4xNmJvd2v61YOYmbvPMaiPvPFlkuPDgfmOV9Y8GtdU17lFIagQ2okY6 LXh4UOJth/36S+i4JPbQOil2kO0jnTzUzZ5o7L3+Irp1hYi9g5SllPVaJfx/T9HHxJFY DK79vngY9XgUv9Fqs79TY+RogaI8kyYOFwPIe0a2HdgmcUlT7B3EplIxawow/Omn8O8o 2v5VHmG1Wnw5L7erkHClbPtn9FggPSsCBcx9OGO9HSKXx4a1fGrQbVnSiuMy5qWNdY/G IgFl7nHt4ZQxabSDiX3iTnni2jfj2xmV6HddpurjdgH5dURBGUGCfD/jMYDodLPgK0X3 vlAg== X-Received: by 10.182.169.68 with SMTP id ac4mr220512obc.6.1359144492874; Fri, 25 Jan 2013 12:08:12 -0800 (PST) Newsgroups: comp.lang.python Date: Fri, 25 Jan 2013 12:08:12 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=94.209.206.24; posting-account=gpkyRAoAAABlPh1mY6Zt264UpMjIbxAz References: <766ec7eb-ab43-4c17-8073-3a0e6a8b89ea@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 94.209.206.24 MIME-Version: 1.0 Subject: Re: finding abc's From: lars van gemerden To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 X-Gm-Message-State: ALoCoQkkbbeRnNZSwLRpXbqzqnrll/DS5U1ARRw5i1k6nA+KD1oMzf52AMmbFVAs2GU/tQq9zI/q Cc: python-list@python.org 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: , Message-ID: Lines: 116 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359144501 news.xs4all.nl 6971 [2001:888:2000:d::a6]:42210 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37693 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 > > 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. Thank you, interesting example. Added confirmation that trying to get the abc's is a bad idea.