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


Groups > comp.lang.python > #4283

Re: Composition instead of inheritance

Date 2011-04-28 18:43 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Composition instead of inheritance
References <d34a5410-42a1-44f6-91b4-32853ae55aca@glegroupsg2000goo.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.971.1304040768.9059.python-list@python.org> (permalink)

Show all headers | View raw


Carl Banks wrote:
> 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.

Hmmm. Okay -- any ideas for a better term?  Something that describes 
taking different source classes and fusing them into a new whole, 
possibly using single-inheritance... Frankenstein, maybe?  ;)


> 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.

 From thread 'python and super' on Python-Dev:
Ricardo Kirkner wrote:
 > I'll give you the example I came upon:
 >
 > I have a TestCase class, which inherits from both Django's TestCase
 > and from some custom TestCases that act as mixin classes. So I have
 > something like
 >
 > class MyTestCase(TestCase, Mixin1, Mixin2):
 >    ...
 >
 > now django's TestCase class inherits from unittest2.TestCase, which we
 > found was not calling super.

This is the type of situation the decorator was written for (although 
it's too simplistic to handle that exact case, as Ricardo goes on to say 
he has a setUp in each mixin that needs to be called -- it works fine 
though if you are not adding duplicate names).

~Ethan~

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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 Ethan Furman <ethan@stoneleaf.us> - 2011-04-28 18:43 -0700
    Re: Composition instead of inheritance Ben Finney <ben+python@benfinney.id.au> - 2011-04-29 13:44 +1000
      Re: Composition instead of inheritance Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-04-29 11:23 +0200
  Re: Composition instead of inheritance MRAB <python@mrabarnett.plus.com> - 2011-04-29 03:14 +0100
  Re: Composition instead of inheritance James Mills <prologic@shortcircuit.net.au> - 2011-04-29 13:16 +1000
  Re: Composition instead of inheritance Dan Stromberg <drsalists@gmail.com> - 2011-04-28 20:58 -0700
    Re: Composition instead of inheritance Ben Finney <ben+python@benfinney.id.au> - 2011-04-29 14:14 +1000
  Re: Composition instead of inheritance John Nagle <nagle@animats.com> - 2011-04-28 22:10 -0700
  Re: Composition instead of inheritance Ethan Furman <ethan@stoneleaf.us> - 2011-04-29 13:39 -0700

csiph-web