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


Groups > comp.lang.python > #37687

Re: finding abc's

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
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; 'win32': 0.03; 'elif': 0.04; 'class,': 0.07; 'classes.': 0.07; 'predefined': 0.07; 'python': 0.09; 'closest': 0.09; 'okay': 0.09; 'other,': 0.09; 'subclass': 0.09; 'def': 0.10; 'a(object):': 0.16; 'b):': 0.16; 'classes:': 0.16; 'cls': 0.16; 'set()': 0.16; 'wrote:': 0.17; 'removed.': 0.17; 'jan': 0.18; '>>>': 0.18; 'module': 0.19; 'all,': 0.21; 'bit': 0.21; 'import': 0.21; 'work.': 0.23; 'this:': 0.23; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'common': 0.26; 'am,': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; '(maybe': 0.29; 'van': 0.29; 'class': 0.29; 'classes': 0.30; 'fri,': 0.30; 'function': 0.30; 'expect': 0.31; 'code': 0.31; 'print': 0.32; 'to:addr:python-list': 0.33; 'that,': 0.34; 'received:google.com': 0.34; 'nov': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'loaded': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; '2010,': 0.38; 'to:addr:python.org': 0.39; 'your': 0.60; 'skip:n 10': 0.63; 'more': 0.63; 'account': 0.67; 'hoping': 0.72; '2.7.1': 0.84; '2013': 0.84; "class's": 0.84; 'to:name:python': 0.84; 'technically': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=Gm7Nnz7Pa3gtnFmGchj1jW5X/QCTU6UQYzxg1L/6jqY=; b=pp2b7KYAczdLh7TN/gi58ZddYF1Ml7q2NWdMBjRV/qMYpNRymEk2/u0pWgQYsefm+m ca50ULdpQXyrFINtcgdcFfJ//AsvxHPNpUpN6t/nPV6zFr+zCie5yXdF8sy7TRBvk1kO DLmqZJlzswhtefz09slhg7Ek25IkOdHI/razzUkLSd36t2ordYrS1+GbdiZ6lW6eWTZw MrQLIuP6oOm6kki43u+vADCrT6V6egr68Z4au2+hN7CS5neM6V/q0IyorgBhAPaxEvtq 7tv/++svInD+/a00rgCL9B0QJrDqC0EBptuyN3hzeGixgUYiNg9Lx8curLmlPmOkT1PB T9rg==
X-Received by 10.68.236.2 with SMTP id uq2mr16524601pbc.55.1359140702156; Fri, 25 Jan 2013 11:05:02 -0800 (PST)
MIME-Version 1.0
In-Reply-To <766ec7eb-ab43-4c17-8073-3a0e6a8b89ea@googlegroups.com>
References <766ec7eb-ab43-4c17-8073-3a0e6a8b89ea@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 25 Jan 2013 12:04:32 -0700
Subject Re: finding abc's
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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.1051.1359140711.2939.python-list@python.org> (permalink)
Lines 59
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1359140711 news.xs4all.nl 6893 [2001:888:2000:d::a6]:52194
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:37687

Show key headers only | View raw


On Fri, Jan 25, 2013 at 10:40 AM, lars van gemerden
<lars@rational-it.com> wrote:
> Hi all,
>
> i was writing a function to determine the common base class of a number classes:
>
[...]
>
> 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)?


If the abstract base class's module has not been imported, it may not
even be loaded into memory, even though it is technically considered a
superclass.  Consider this:


Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def common_base(classes):
...     common = set()
...     for cls in object.__subclasses__():
...         if all(issubclass(c, cls) for c in classes):
...             common.add(cls)
...     return common
...
>>> common_base([int, float])
set([<class '_abcoll.Hashable'>])
>>> import numbers
>>> common_base([int, float])
set([<class 'numbers.Number'>, <class '_abcoll.Hashable'>])


If you're okay with that, then the approach above might work.


>     while len(common) > 1:
>         cls1 = common.pop()
>         cls2 = common.pop()
>         if issubclass(cls1, cls2):
>             common.add(cls1)
>         elif issubclass(cls2, cls1):
>             common.add(cls2)

There is a flaw with your set reduction code here.  If neither class
is a subclass of the other, then both will be removed.  There may not
actually be a single closest common base class, however.  What would
you expect the function to return in the following situation?

class A(object): pass
class B(object): pass
class C(A, B): pass
class D(A, B): pass

print common_base([C, D])

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