Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43092
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <arnodel@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.036 |
| X-Spam-Evidence | '*H*': 0.93; '*S*': 0.00; 'else:': 0.03; '(python': 0.07; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'def': 0.12; 'one)': 0.16; 'subclass': 0.16; 'wrote:': 0.18; 'code.': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'define': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'received:209.85.217': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'class': 0.32; 'skip:- 30': 0.32; 'could': 0.34; 'received:209.85': 0.35; 'classes': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'received:209': 0.37; 'skip:p 20': 0.39; 'course': 0.61; 'such': 0.63; 'different': 0.65; 'here': 0.66; 'of:': 0.68; '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=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=e58oDXyCB9gjTtokVkNQbpdWiBfgy/zEupFGJG+cBik=; b=UyK94jIEkwEvmkfem1ovwZisGKQsaWvU6YnYbD5FGj+YnklQyLtI9y7PC23aIUFRFC VdP/RshmXPQoNL/rPX6/QeqTxcqN0LCpPNgMST5NL6/o3HeNnD8aij9BVsycZK0V9578 4Ega4TId/NGD4rnpR4CgYfu3MYFUn7GVE1CkY62jGw+3KfC1EJs/llHwFIl+odx1jlpx OWuqVzYsX5AulYfqqz++Av8LxR36o6Ren8pptYXuBGn1LLlVssZenUWak5/xZJM29X7w 0Hrti41+BRlK+E+bFP/u8VW0cpQgeT1iI3TkVldgJHjld4HhXpBt3gtZOHzURerKAUvk uZDQ== |
| MIME-Version | 1.0 |
| X-Received | by 10.112.133.72 with SMTP id pa8mr9013992lbb.114.1365455823967; Mon, 08 Apr 2013 14:17:03 -0700 (PDT) |
| In-Reply-To | <51629193.6050301@rece.vub.ac.be> |
| References | <51629193.6050301@rece.vub.ac.be> |
| Date | Mon, 8 Apr 2013 22:17:03 +0100 |
| Subject | Re: How to subclass a family |
| From | Arnaud Delobelle <arnodel@gmail.com> |
| To | Antoon Pardon <antoon.pardon@rece.vub.ac.be> |
| Content-Type | text/plain; charset=UTF-8 |
| Cc | Python <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.302.1365455826.3114.python-list@python.org> (permalink) |
| Lines | 70 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1365455826 news.xs4all.nl 6973 [2001:888:2000:d::a6]:38133 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:43092 |
Show key headers only | View raw
On 8 April 2013 10:44, Antoon Pardon <antoon.pardon@rece.vub.ac.be> wrote:
> Here is the idea. I have a number of classes with the same interface.
> Something like the following:
>
> class Foo1:
> def bar(self, ...):
> work
> def boo(self, ...):
> do something
> self.bar(...)
>
> What I want is the equivallent of:
>
> class Far1(Foo1):
> def boo(self, ...)
> do something different
> if whatever:
> self.bar(...)
> else:
> Foo1.boo(self, ...)
>
> 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?
>
(Python 3)
------------------------------
class Foo1:
def bar(self):
print('Foo1.bar')
def boo(self, whatever):
print('Foo1.boo', whatever)
self.bar()
# class Foo2: ...(I'll let you define this one)
class DifferentBoo:
def boo(self, whatever):
print('DifferentBoo.boo', whatever)
if whatever:
self.bar()
else:
super().boo(whatever)
class Far1(DifferentBoo, Foo1): pass
# class Far2(DifferentBoo, Foo2): pass
------------------------------
>>> foo = Foo1()
>>> foo.bar()
Foo1.bar
>>> foo.boo(1)
Foo1.boo 1
Foo1.bar
>>> far = Far1()
>>> far.bar()
Foo1.bar
>>> far.boo(0)
DifferentBoo.boo 0
Foo1.boo 0
Foo1.bar
>>> far.boo(1)
DifferentBoo.boo 1
Foo1.bar
HTH,
--
Arnaud
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: How to subclass a family Arnaud Delobelle <arnodel@gmail.com> - 2013-04-08 22:17 +0100
csiph-web