Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #4277 > unrolled thread
| Started by | Carl Banks <pavlovevidence@gmail.com> |
|---|---|
| First post | 2011-04-28 15:35 -0700 |
| Last post | 2011-04-28 20:57 -0700 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | Carl Banks <pavlovevidence@gmail.com> |
|---|---|
| Date | 2011-04-28 15:35 -0700 |
| Subject | Re: Composition instead of inheritance |
| Message-ID | <mailman.967.1304030151.9059.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-04-29 02:31 +0000 |
| Message-ID | <4dba22f3$0$29978$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #4277 |
On Thu, 28 Apr 2011 15:35:47 -0700, Carl Banks wrote: > 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. I thought that was delegation. As in, when one object delegates some or all of it's behaviour to another object: http://code.activestate.com/recipes/52295 > Often used to model a part/whole relationship, hence the name. In mathematics, composition means to make a function by applying a function to the output of another. E.g.: f o g(x) is equivalent to f(g(x)) (Strictly speaking, the "o" in the f o g should be a little circle rather than lowercase "O".) -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2011-04-29 13:50 +1000 |
| Message-ID | <87k4edk95u.fsf@benfinney.id.au> |
| In reply to | #4288 |
Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes: > On Thu, 28 Apr 2011 15:35:47 -0700, 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. > > I thought that was delegation. As in, when one object delegates some or > all of it's behaviour to another object: > > http://code.activestate.com/recipes/52295 The difference being that, with delegation, the object being called is unrelated to this one. With composition, the object being called is not wholly separate, but is instead an attribute on the current object. The “another object that it owns” was, I presume, meant to communicate this “an object which is an attribute of the current one” relationship. > In mathematics, composition means to make a function by applying a > function to the output of another. The term “composition” in the programming sense comes from OO design, not functional nor mathematical terminology. -- \ “I don't like country music, but I don't mean to denigrate | `\ those who do. And for the people who like country music, | _o__) denigrate means ‘put down’.” —Bob Newhart | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2011-04-28 20:57 -0700 |
| Message-ID | <mailman.980.1304049437.9059.python-list@python.org> |
| In reply to | #4288 |
On 29 Apr 2011 02:31:15 GMT, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> declaimed the following in
gmane.comp.python.general:
>
> I thought that was delegation. As in, when one object delegates some or
> all of it's behaviour to another object:
>
"Delegation" sounds more like functional behavior whereas
"composition" is more the static organization -- that is, which objects
are components of the overall object.
Just a gut feeling...
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web