Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37688

Re: finding abc's

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 <python-python-list@m.gmane.org>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1052.1359140888.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

finding abc's lars van gemerden <lars@rational-it.com> - 2013-01-25 09:40 -0800
  Re: finding abc's Ian Kelly <ian.g.kelly@gmail.com> - 2013-01-25 12:04 -0700
    Re: finding abc's lars van gemerden <lars@rational-it.com> - 2013-01-25 12:05 -0800
    Re: finding abc's lars van gemerden <lars@rational-it.com> - 2013-01-25 12:05 -0800
  Re: finding abc's Peter Otten <__peter__@web.de> - 2013-01-25 20:08 +0100
    Re: finding abc's lars van gemerden <lars@rational-it.com> - 2013-01-25 12:08 -0800
      Re: finding abc's lars van gemerden <lars@rational-it.com> - 2013-01-25 16:48 -0800
      Re: finding abc's lars van gemerden <lars@rational-it.com> - 2013-01-25 16:48 -0800
    Re: finding abc's lars van gemerden <lars@rational-it.com> - 2013-01-25 12:08 -0800

csiph-web