Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37691
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.straub-nv.de!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 | <lars@rational-it.com> |
| 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; 'win32': 0.03; 'elif': 0.04; 'class,': 0.07; 'classes.': 0.07; 'predefined': 0.07; 'python': 0.09; 'closest': 0.09; 'friday,': 0.09; 'okay': 0.09; 'other,': 0.09; 'subclass': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'a(object):': 0.16; 'algorithm.': 0.16; 'b):': 0.16; 'classes:': 0.16; 'cls': 0.16; 'set()': 0.16; 'wrote:': 0.17; 'removed.': 0.17; 'thanks,': 0.18; 'jan': 0.18; '>>>': 0.18; 'module': 0.19; 'all,': 0.21; 'trying': 0.21; 'bit': 0.21; 'import': 0.21; 'cc:2**0': 0.23; 'work.': 0.23; 'this:': 0.23; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'common': 0.26; 'am,': 0.27; 'question': 0.27; '(maybe': 0.29; 'concise': 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; 'that,': 0.34; 'received:google.com': 0.34; 'nov': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'loaded': 0.36; "i'll": 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; '2010,': 0.38; 'your': 0.60; 'skip:n 10': 0.63; 'more': 0.63; 'account': 0.67; 'answer.': 0.71; 'hoping': 0.72; '2.7.1': 0.84; '2013': 0.84; "class's": 0.84; 'technically': 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=e+CEst08if8jkLLO6TZAArirrHudWIFo6BPq4ZSnSM8=; b=hUHOOxZoZSE2ae9U1+LX9QDa2GvLsNerOYgGhxzXGxP6KqzJp1+21E2UesSofpTeMY cKDDnsqc38nKTmJUzEwE8g7/cdeBhHdpYvZbWUltVgwVwRwH3UMAayi68zf543v/BJMJ I90n4mNinIPAfu8gubSYGkW+io+mMqdiLXIVTdk1U9f+ygyUG+tVXif08cOTkZ7hdd5o TqVxXOnppVXYuSibb2bI+puwF+WAmgFB878cAaFdvKZnt7rgzw6zuLWVyDFeBTFaMpAc DRUQ3CVJpNYMIBYteFYEkKEBPEBnC+5Vjki5NpIYDbFz7/QceR6UxjYJoDqmvMC6XgJC s3NA== |
| X-Received | by 10.49.15.6 with SMTP id t6mr1524648qec.20.1359144335069; Fri, 25 Jan 2013 12:05:35 -0800 (PST) |
| Newsgroups | comp.lang.python |
| Date | Fri, 25 Jan 2013 12:05:34 -0800 (PST) |
| In-Reply-To | <mailman.1051.1359140711.2939.python-list@python.org> |
| 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> <mailman.1051.1359140711.2939.python-list@python.org> |
| 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 <lars@rational-it.com> |
| To | comp.lang.python@googlegroups.com |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-Gm-Message-State | ALoCoQk6Y+GyVf1ArqHhyzBBZP9Re4DZKkGZi4eVwBstLrJpk5Q481qR2ShwYQkAfY+r5zqizNF4 |
| Cc | Python <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 <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> |
| Message-ID | <mailman.1054.1359144344.2939.python-list@python.org> (permalink) |
| Lines | 120 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1359144344 news.xs4all.nl 6899 [2001:888:2000:d::a6]:39528 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:37691 |
Show key headers only | View raw
On Friday, January 25, 2013 8:04:32 PM UTC+1, Ian wrote: > 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]) thanks, good catch, and very concise answer. I'll give up on trying to get abc's and improve my algorithm.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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