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


Groups > comp.lang.python > #42683

Re: Mixin way?

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 <andrea.crotti.0@gmail.com>
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 <mailman.60.1364997899.3114.python-list@python.org> <515c4f07$0$29966$c3e8da3$5496439d@news.astraweb.com>
Date Wed, 3 Apr 2013 17:29:02 +0100
Subject Re: Mixin way?
From andrea crotti <andrea.crotti.0@gmail.com>
To "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
Content-Type multipart/alternative; boundary=f46d044286ca1a028204d9775ac9
Cc python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
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>
Newsgroups comp.lang.python
Message-ID <mailman.64.1365006544.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

2013/4/3 Steven D'Aprano <steve+comp.lang.python@pearwood.info>

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

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


Thread

Mixin way? andrea crotti <andrea.crotti.0@gmail.com> - 2013-04-03 15:04 +0100
  Re: Mixin way? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-03 15:47 +0000
    Re: Mixin way? andrea crotti <andrea.crotti.0@gmail.com> - 2013-04-03 17:29 +0100
      Re: Mixin way? Neil Cerutti <neilc@norwich.edu> - 2013-04-03 17:16 +0000

csiph-web