Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'skip:[ 20': 0.04; '"""': 0.07; 'attribute': 0.07; 'defaults': 0.07; 'attributes': 0.09; 'constructor': 0.09; 'objects,': 0.09; 'cc:addr:python-list': 0.11; 'clear.': 0.16; 'dictionaries': 0.16; 'optional': 0.16; 'simpler,': 0.16; 'subclass': 0.16; 'subclassing': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; '8bit%:5': 0.22; 'cc:addr:python.org': 0.22; 'fine': 0.24; 'cc:2**0': 0.24; 'values': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'class': 0.32; 'probably': 0.32; 'received:74.125.82': 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'right?': 0.36; 'done': 0.36; 'subject:?': 0.36; 'so,': 0.37; 'too': 0.37; 'list': 0.37; 'skip:& 10': 0.38; 'auto': 0.38; 'filled': 0.38; 'ability': 0.39; 'explain': 0.39; 'skip:& 20': 0.39; 'received:74.125': 0.39; 'more': 0.64; 'different': 0.65; 'situation': 0.65; 'between': 0.67; 'design.': 0.68; 'default': 0.69; 'couch': 0.84; 'duplication': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=sQS4fId2pJvLJGFPubv5FV7Jz0BDuqO5L9tL1uUOz58=; b=PfEvfnwQ9Dr1eGrF8o5UOVwlaQtpBv7GGgXq4r+MvythbHjDYR9jjYButrUufamzlx EUL7/P2UyZ+T/uiY+QVg32vOrIDWtYNpIcP7RdzByPX33c5e4UVksg21MOLogkMdU0NM 7Ys9AD+Sx5gFhy+w8iVb2jrafZAP749DywhtZjM4k1ZqIabw0KnCzMHUGFXyp1KjpPBJ PXw3gFjMacj+00tDQ7AnuEVVVFBpg4wjgLSnpHa/dNFRnQdkwu5CuhbRm2UiRpH9Rmax dl0ggYANzAKVB1NYM8GY0Q3UL/seGYFm6lIDFMiM72yZyzFmTgmbleLpxtVE2x2sOEFc AVYA== MIME-Version: 1.0 X-Received: by 10.180.98.198 with SMTP id ek6mr3857374wib.7.1365006542828; Wed, 03 Apr 2013 09:29:02 -0700 (PDT) In-Reply-To: <515c4f07$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <515c4f07$0$29966$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 3 Apr 2013 17:29:02 +0100 Subject: Re: Mixin way? From: andrea crotti To: "Steven D'Aprano" Content-Type: multipart/alternative; boundary=f46d044286ca1a028204d9775ac9 Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 102 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365006544 news.xs4all.nl 6902 [2001:888:2000:d::a6]:38586 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42683 --f46d044286ca1a028204d9775ac9 Content-Type: text/plain; charset=ISO-8859-1 2013/4/3 Steven D'Aprano > [snip] > > So, if you think of "Visitable" as a gadget that can be strapped onto > your MyObj as a component, then composition is probably a better design. > But if you think of "Visitable" as a mere collection of behaviour and > state, then a mixin is probably a better design. > > Well I can explain better the situation to make it more clear. We are using CouchDb and so far it has been (sigh) a brutal manipulation of dictionaries everywhere, with code duplication and so on. Now I wanted to encapsulate all the entities in the DB in proper objects, so I have a CouchObject: class CouchObject(object) : """ Encapsulate an object which has the ability to be saved to a couch database. """ #: list of fields that get filled in automatically if not passed in AUTO = ['created_datetime', 'doc_type', '_id', '_rev'] #: dictionary with some extra fields with default values if not # passed in the constructor the default value gets set to the attribute DEFAULTS = {} REQUIRED = [] OPTIONAL = [] TO_RESOLVE = [] MIXINS = [] Where every subclass can redefine these attributes to get something done automatically by the constructor for convenience. Now however there is a lot of behaviour shared between them, so I want to encapsulate it out in different places. I think the MIXINS as I would use it is the normal composition pattern, the only difference is that the composition is done per class and not per object (again for lazyness reasons), right? Probably subclassing might be fine as well, and makes it simpler, but I don't like too much to do subclass from multiple classes... --f46d044286ca1a028204d9775ac9 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
2013/4/3 Steven D'Aprano <steve+co= mp.lang.python@pearwood.info>
<= div class=3D"gmail_quote">
[snip]

So, if you think of "Visitable" as a gadget that can be strapped = onto
your MyObj as a component, then composition is probably a better design. But if you think of "Visitable" as a mere collection of behaviour= and
state, then a mixin is probably a better design.


Well I= can explain better the situation to make it more clear.

We are using CouchDb and so far it has been (sigh) a brutal manipulati= on of dictionaries everywhere, with code duplication and so on.
<= br>
Now I wanted to encapsulate all the entities in the DB in pro= per objects, so I have a CouchObject:


class CouchObject(object) :
= =A0 =A0 """
=A0 =A0 Encapsulate an object which ha= s the ability to be saved to a couch
=A0 =A0 database.
= =A0 =A0 """
=A0 =A0 #: list of fields that get filled in automatically if not pass= ed in
=A0 =A0 AUTO =3D ['created_datetime', 'doc_type= ', '_id', '_rev']
=A0 =A0 #: dictionary with = some extra fields with default values if not
=A0 =A0 # passed in the constructor the default value gets set to the = attribute
=A0 =A0 DEFAULTS =3D {}
=A0 =A0 REQUIRED =3D = []
=A0 =A0 OPTIONAL =3D []
=A0 =A0 TO_RESOLVE =3D []
=A0 =A0 MIXINS =3D []
=A0
Where every subclass can redefine these attributes to ge= t something
done automatically by the constructor for convenience= .

Now however there is a lot of behaviour shared b= etween them, so I want
to encapsulate it out in different places.

I = think the MIXINS as I would use it is the normal composition
patt= ern, the only difference is that the composition is done per class
and not per object (again for lazyness reasons), right?

=
Probably subclassing might be fine as well, and makes it simpler= , but
I don't like too much to do subclass from multiple clas= ses...=A0
--f46d044286ca1a028204d9775ac9--