Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'duplicate': 0.07; 'decorator': 0.09; 'method:': 0.09; 'properly.': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'def': 0.12; 'preserve': 0.16; 'subclass': 0.16; 'subclasses': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'cc:addr:python.org': 0.22; 'adds': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'said,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'class': 0.32; 'stuff': 0.32; 'could': 0.34; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'in:': 0.36; 'received:209': 0.37; 'little': 0.38; 'even': 0.60; 'course': 0.61; 'such': 0.63; 'different': 0.65; 'family': 0.73; 'pardon': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=owvNB1mQNxGoLwq1PCARIx2fphjLc3So9k9R7+2ZY8I=; b=clRWfw23OBAqauC3ACQBFNT+fZNY0ImmGpwb6N6sH+cSvEAhu/gVNUMssnw0FrxV6M kxO/Xloj9Et+fx66X7qUWx0bsVgRfGljjFDgtkS1Zibr7bjUHW9/26lxpF3zYqEgq0qZ bk6rSw7yxz36Iq6f+5UOmda8k6xMSRThY0HJlUC0qKp+km+ldquhvuwLcSzI4Oz895wJ zgubYmanP5EkusOVogO8LGXAEUbwZDDulrbiRWwd0xP7UZRNIgC8VsAOE57cADuj8j/+ eKsHEStR2ttlIv7NDLwZOBYJ4HBHVRLQ5w6YjReRKkRHA1mu9ASQ9gJ5ZE7+NYB/tpJX obIA== X-Received: by 10.220.9.3 with SMTP id j3mr17439615vcj.56.1365463376183; Mon, 08 Apr 2013 16:22:56 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <51629193.6050301@rece.vub.ac.be> References: <51629193.6050301@rece.vub.ac.be> From: Devin Jeanpierre Date: Mon, 8 Apr 2013 19:22:16 -0400 Subject: Re: How to subclass a family To: Antoon Pardon Content-Type: text/plain; charset=UTF-8 Cc: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365463379 news.xs4all.nl 6869 [2001:888:2000:d::a6]:45599 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43100 On Mon, Apr 8, 2013 at 5:44 AM, Antoon Pardon wrote: > Now of course I could subclass every class from the original family > from Foo1 to Foon but that would mean a lot of duplicated code. Is > there a way to reduce the use of duplicated code in such circumstances? As a rule, if there's duplicate code you can stuff it in a function. def create_subclass(Foo): class Far(Foo): def boo(self, ...) do something different if whatever: self.bar(...) else: super(Far, self).boo(self, ...) return Far Far1 = create_subclass(Foo1) Far2 = create_subclass(Foo2) ... Of course, this doesn't preserve the names of the subclasses properly. To do that you can add a parameter, for the name, although this is a little repetitive. Alternatively you can subclass yet again, as in: class Far1(create_subclass(Foo1)): pass Or you can even change the approach to a class decorator that adds a method: def add_method(cls): def boo(self, ...): do something different if whatever: self.bar(...) else: super(cls, self).boo(...) @add_method class Far1(Foo1): pass @add_method class Far2(Foo2): pass As a wise man once said, TIMTOWTDI. :( -- Devin