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: 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 To: python-list 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --f46d043c7f0479487604d97556f3 Content-Type: text/plain; charset=ISO-8859-1 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? --f46d043c7f0479487604d97556f3 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
I have some classes that have shared behaviours, for = example in our
scenario an object can be "visited", whe= re something that is visitable
would have some behaviour like

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

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

Where the Mixin class simply initialises the attributes= :

--8<---------------cut here---------------sta= rt------------->8---
class Mixin(object):
=A0 =A0 de= f __init__(self, **kwargs):
=A0 =A0 =A0 =A0 for key, val in self.FIELDS.items():
=A0 =A0= =A0 =A0 =A0 =A0 setattr(self, key, val)

=A0 =A0 = =A0 =A0 for key, val in kwargs.items():
=A0 =A0 =A0 =A0 =A0 =A0 i= f key in self.FIELDS:
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 setattr(sel= f, key, val)
--8<---------------cut here---------------end--------------->8--= -


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

class MyObjectBase(object):
=A0 =A0pass

=
class MyObj(MyObjectBase, Visitable):
=A0 =A0pass

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

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

class ObjectWithMixin(CouchObject):
=A0 =A0 M= IXINS =3D [Visitable]

and then do all the smart th= ings needed:
- at object construction time
- when setti= ng attributes and so on..

This solution is more complicated to implement but mayb= e is more
flexible and more "correct", what do you thin= k?
--f46d043c7f0479487604d97556f3--