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: 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: 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 To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: 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 On Thursday, April 28, 2011 10:15:02 AM UTC-7, Ethan Furman wrote: > For anybody interested in composition instead of multiple inheritance, I= =20 > have posted this recipe on ActiveState (for python 2.6/7, not 3.x): >=20 > http://code.activestate.com/recipes/577658-composition-of-classes-instead= -of-multiple-inherit/ >=20 > Comments welcome! That's not what we mean by composition. Composition is when one object cal= ls upon another object that it owns to implement some of its behavior. Oft= en used to model a part/whole relationship, hence the name. The sorts of class that this decorator will work for are probably not the o= nes 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 cod= e will have a common, well-known Python construct rather than a custom deco= rator to understand. If you want to enforce no duplication of attributes you can do that, such a= s with this untested metaclass: class MakeSureNoBasesHaveTheSameClassAttributesMetaclass(type): def __new__(metatype,name,bases,dct): u =3D collections.Counter() for base in bases: for key in base.__dict__.keys(): u[key] +=3D 1 for key in dct.keys(): u[key] +=3D 1 if any(u[key] > 1 for key in u.keys()): raise TypeError("base classes and this class share some class a= ttributes") return type.__new__(metatype,name,bases,dct) =20 Carl Banks