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


Groups > comp.lang.python > #42670

Mixin way?

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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.066
X-Spam-Evidence '*H*': 0.87; '*S*': 0.00; '[],': 0.09; 'attributes': 0.09; 'val': 0.09; 'def': 0.12; '**kwargs):': 0.16; 'attribute:': 0.16; 'attributes:': 0.16; 'semantically': 0.16; 'subclasses': 0.16; 'subclassing': 0.16; 'think?': 0.16; 'seems': 0.21; '8bit%:5': 0.22; 'example': 0.22; 'example.': 0.24; 'pass': 0.26; '8bit%:3': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '(which': 0.31; "skip:' 10": 0.31; 'easy,': 0.31; 'though.': 0.31; 'class': 0.32; 'probably': 0.32; 'another': 0.32; 'quite': 0.32; 'to:name:python-list': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; 'classes': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'done': 0.36; 'subject:?': 0.36; 'skip:- 20': 0.37; 'implement': 0.38; 'skip:o 20': 0.38; 'to:addr :python-list': 0.38; 'skip:& 20': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; '8bit%:6': 0.40; 'how': 0.40; 'skip:o 30': 0.61; 'simply': 0.61; 'our': 0.64; 'more': 0.64; 'different': 0.65; 'skip:1 20': 0.65; 'construction': 0.72; 'needed:': 0.84; 'now)': 0.84; 'partially': 0.84; 'who,': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=JSk65N8agrzmUSwzZVqj/d80SQwvtK83Tg6HRi2OQjA=; b=ssrzAIDiiGxHWd5F2fUUfwe2+aTTTHqkk9hWOcD8izItrnbp7NGMLfnjHwIwTyzBaw N3gOQmNGvFLMKqznL0CdxWfXt50L0Ryj+k64v556GtBMdAKzN2LZ66VUeXjjaW2W6ZVB oQbo69vM2s4k5P704sAvbo06AaNnwlKRNT9mNax3zuDeDVGD0VAb+9xFIPMEwm0ajxyR FznImytHbnm17DIGqrI5qYusnQMV9Ms33Au2+SzlaevzOm9M+g7MlBgTWIoBFjwBLYh/ 5FByftysPngAD7aPzvt06eXTP4bV19y5llpYUfW0NmWmG/d59dhNAJUAqgD3fel/id8y ItpA==
MIME-Version 1.0
X-Received by 10.180.73.6 with SMTP id h6mr22823163wiv.27.1364997892030; Wed, 03 Apr 2013 07:04:52 -0700 (PDT)
Date Wed, 3 Apr 2013 15:04:51 +0100
Subject Mixin way?
From andrea crotti <andrea.crotti.0@gmail.com>
To python-list <python-list@python.org>
Content-Type multipart/alternative; boundary=f46d043c7f0479487604d97556f3
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.60.1364997899.3114.python-list@python.org> (permalink)
Lines 106
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364997899 news.xs4all.nl 6952 [2001:888:2000:d::a6]:40087
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42670

Show key headers only | View raw


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

I have some classes that have shared behaviours, for example in our
scenario an object can be "visited", where something that is visitable
would have some behaviour like

--8<---------------cut here---------------start------------->8---
class Visitable(Mixin):
    FIELDS = {
        'visits': [],
        'unique_visits': 0,
    }

    def record_view(self, who, when):
        self.visits += {'who': who, 'when': when}
        self.unique_visits += 1
--8<---------------cut here---------------end--------------->8---

Where the Mixin class simply initialises the attributes:

--8<---------------cut here---------------start------------->8---
class Mixin(object):
    def __init__(self, **kwargs):
        for key, val in self.FIELDS.items():
            setattr(self, key, val)

        for key, val in kwargs.items():
            if key in self.FIELDS:
                setattr(self, key, val)
--8<---------------cut here---------------end--------------->8---


So now I'm not sure how to use it though.
One way would be multiple subclasses

class MyObjectBase(object):
   pass

class MyObj(MyObjectBase, Visitable):
   pass

for example.
This solution is probably easy, but at the same time disturbing because
MyObjectBase is semantically quite different from Visitable, so
subclassing from both seems wrong..

The other solution (which is what is partially done now) is to use
another class attribute:

class ObjectWithMixin(CouchObject):
    MIXINS = [Visitable]

and then do all the smart things needed:
- at object construction time
- when setting attributes and so on..

This solution is more complicated to implement but maybe is more
flexible and more "correct", what do you think?

Back to comp.lang.python | Previous | NextNext 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