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


Groups > comp.lang.python > #4277

Re: Composition instead of inheritance

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <pavlovevidence@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.025
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'sorts': 0.04; 'attributes': 0.05; 'behavior.': 0.07; 'recipe': 0.07; 'python': 0.07; 'decorator': 0.09; 'url:activestate': 0.09; 'url:code': 0.11; 'def': 0.13; 'wrote:': 0.14; 'furman': 0.16; 'cc:2**0': 0.20; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:addr:python- list': 0.22; 'posted': 0.22; 'trying': 0.23; 'instead': 0.26; 'classes': 0.26; 'object': 0.27; 'skip:b 20': 0.27; 'raise': 0.29; 'class': 0.29; 'probably': 0.30; 'implement': 0.30; 'cc:addr:python.org': 0.31; 'model': 0.31; 'carl': 0.31; 'construct': 0.31; 'another': 0.32; '(for': 0.33; 'that,': 0.35; 'header:User-Agent:1': 0.35; 'rather': 0.36; 'problems': 0.37; 'some': 0.37; 'received:209.85': 0.37; 'thursday,': 0.38; 'received:google.com': 0.38; 'used': 0.38; 'comments': 0.39; 'received:209': 0.39; 'might': 0.40; 'owns': 0.60; '2011': 0.62; 'upon': 0.63; 'share': 0.67; 'to:addr:googlegroups.com': 0.69; 'reply-to:no real name:2**0': 0.72; 'header:Reply-To:1': 0.72; 'common,': 0.84; 'composition': 0.84; 'inheritance,': 0.91; 'received:209.85.210.56': 0.91; 'received:mail- pz0-f56.google.com': 0.91; 'reply-to:addr:googlegroups.com': 0.93
Newsgroups comp.lang.python
Date Thu, 28 Apr 2011 15:35:47 -0700 (PDT)
In-Reply-To <mailman.946.1304010200.9059.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=157.127.155.214; posting-account=XFEWnwoAAADNO10m3Wcmq_SWdmyZuXff
User-Agent G2/1.0
MIME-Version 1.0
Subject Re: Composition instead of inheritance
From Carl Banks <pavlovevidence@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc Python <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To comp.lang.python@googlegroups.com
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.967.1304030151.9059.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 82.94.164.166
X-Trace 1304030151 news.xs4all.nl 41110 [::ffff:82.94.164.166]:37393
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:4277

Show key headers only | View raw


On Thursday, April 28, 2011 10:15:02 AM UTC-7, Ethan Furman wrote:
> For anybody interested in composition instead of multiple inheritance, I 
> have posted this recipe on ActiveState (for python 2.6/7, not 3.x):
> 
> http://code.activestate.com/recipes/577658-composition-of-classes-instead-of-multiple-inherit/
> 
> Comments welcome!

That's not what we mean by composition.  Composition is when one object calls upon another object that it owns to implement some of its behavior.  Often used to model a part/whole relationship, hence the name.

The sorts of class that this decorator will work for are probably not the ones that are going to have problems cooperating in the first place.  So you might as well just use inheritance; that way people trying to read the code will have a common, well-known Python construct rather than a custom decorator to understand.

If you want to enforce no duplication of attributes you can do that, such as with this untested metaclass:

class MakeSureNoBasesHaveTheSameClassAttributesMetaclass(type):
    def __new__(metatype,name,bases,dct):
        u = collections.Counter()
        for base in bases:
            for key in base.__dict__.keys():
                u[key] += 1
        for key in dct.keys():
            u[key] += 1
        if any(u[key] > 1 for key in u.keys()):
            raise TypeError("base classes and this class share some class attributes")
        return type.__new__(metatype,name,bases,dct)
 

Carl Banks

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


Thread

Re: Composition instead of inheritance Carl Banks <pavlovevidence@gmail.com> - 2011-04-28 15:35 -0700
  Re: Composition instead of inheritance Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-29 02:31 +0000
    Re: Composition instead of inheritance Ben Finney <ben+python@benfinney.id.au> - 2011-04-29 13:50 +1000
    Re: Composition instead of inheritance Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-04-28 20:57 -0700

csiph-web