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


Groups > comp.lang.python > #7911

Re: What's the best way to write this base class?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.024
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'character,': 0.07; 'instance.': 0.09; 'subclasses': 0.09; 'am,': 0.14; 'wrote:': 0.14; 'class).': 0.16; 'elf': 0.16; 'idea:': 0.16; 'name):': 0.16; 'tempted': 0.16; '\xa0def': 0.16; "wouldn't": 0.17; 'classes,': 0.19; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; "haven't": 0.22; 'maybe': 0.23; 'vs.': 0.23; 'received:209.85.161.46': 0.23; 'received:mail-fx0-f46.google.com': 0.23; '(or': 0.24; '(e.g.': 0.26; 'received:209.85.161': 0.26; 'says': 0.27; 'message- id:@mail.gmail.com': 0.28; 'character': 0.29; 'sat,': 0.29; 'subject:?': 0.29; 'explicitly': 0.29; 'toward': 0.29; 'class': 0.29; 'etc.,': 0.30; 'situation,': 0.30; 'equivalent': 0.31; 'to:addr:python-list': 0.33; 'too': 0.33; 'subject:What': 0.35; 'not,': 0.35; 'using': 0.35; 'test': 0.35; 'assigned': 0.37; 'rules': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; 'instead,': 0.37; 'but': 0.38; 'subject:: ': 0.38; 'doing': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'ready': 0.63; 'categories': 0.67; 'yourself': 0.68; 'here.': 0.69; 'race': 0.73; 'subject:this': 0.76; 'composition': 0.84; 'subject:class': 0.84; 'subject:write': 0.84; 'subject:best': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=vJawaySK/H8eQQ3pBoWzvLVGEswjTeP3HeayBjK77Fw=; b=WtizV54TndmQYbZLk9Ad+20nYI95+P8HU+3CQP1VqpZM8kIA2rA/OaQKfvuwEDW1DW ArroQvT2bun/T19f3bHo3HcrY2nfWEvd6XTK8Go+XSdwXAYyPely/lcT7VWPJZ8YJ4Rs vgsMYL19LJehklWilL8yMs/Fnj+4G/pZqmG54=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=gJwb9BKAEGjCibc+nHdoJtb7Q6Nz1PHxRr3XIWDRNY+c0fHK+Rcmuawx40x6ZHOOJb vy3igTErrqDK5CQvIJ+V1orIEl8baKC/hv+9RX7HcsP4KYy6lMPgVQo+D8+jL66dZBLI eHKVUN5C2VSWl1wFAzoxXrYha8CwubrnyADE4=
MIME-Version 1.0
In-Reply-To <6c64a047-f137-4865-9dde-812461a4090e@u10g2000yqh.googlegroups.com>
References <142e76c3-b304-43ef-af24-919fa6146369@c9g2000yqp.googlegroups.com> <e6841cf8-f365-4c23-9307-b69565025203@dq9g2000vbb.googlegroups.com> <mailman.116.1308396299.1164.python-list@python.org> <6c64a047-f137-4865-9dde-812461a4090e@u10g2000yqh.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sat, 18 Jun 2011 08:51:03 -0600
Subject Re: What's the best way to write this base class?
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.121.1308408694.1164.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 82.94.164.166
X-Trace 1308408695 news.xs4all.nl 49178 [::ffff:82.94.164.166]:57827
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7911

Show key headers only | View raw


On Sat, Jun 18, 2011 at 7:37 AM, bruno.desthuilliers@gmail.com
<bruno.desthuilliers@gmail.com> wrote:
> If you go that way, then using polymorphic dispatch might (or not,
> depending on the game's rules <g>) be a good idea:
>
>
> class Character(object):
>    BASE_HEALTH = 50
>    ...
>    def __init__(self, name):
>        ...
>        self.health = type(self).BASE_HEALTH

This of course is equivalent to a simple "self.health =
self.BASE_HEALTH" as long as you haven't explicitly assigned
BASE_HEALTH on the instance.

Tangentially, I wouldn't use inheritance at all for this game.  I know
the classic "is-a / has-a" test says that a wizard "is a" character,
but in my experience that method leans toward doing way too much
inheritance.  If you have subclasses for character classes, then you
will be tempted to also use subclasses for races, and then when you're
ready to make elf wizards you will have forced yourself into a
multiple inheritance situation, and down that path wait Agony and
Despair.

Instead, I would use composition here.  A character has a class (e.g.
Wizard(specialization='fire')) and a race (e.g. Elf(breed='high') --
or maybe just HighElf(), which inherits from Elf).  Save inheritance
for broad categories of what it means to be a character (e.g.
PlayerCharacter vs. NonPlayerCharacter or MobileCharacter vs.
MagicMirror, etc., any of which might have the Wizard character
class).

Cheers,
Ian

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


Thread

What's the best way to write this base class? John Salerno <johnjsal@gmail.com> - 2011-06-17 21:17 -0700
  Re: What's the best way to write this base class? Chris Angelico <rosuav@gmail.com> - 2011-06-18 14:53 +1000
  Re: What's the best way to write this base class? "bruno.desthuilliers@gmail.com" <bruno.desthuilliers@gmail.com> - 2011-06-18 03:55 -0700
    Re: What's the best way to write this base class? Tim Chase <python.list@tim.thechases.com> - 2011-06-18 06:24 -0500
      Re: What's the best way to write this base class? "bruno.desthuilliers@gmail.com" <bruno.desthuilliers@gmail.com> - 2011-06-18 06:37 -0700
        Re: What's the best way to write this base class? Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-18 08:51 -0600
  Re: What's the best way to write this base class? TheSaint <nobody@nowhere.net.no> - 2011-06-18 19:04 +0800
  Re: What's the best way to write this base class? Mel <mwilson@the-wire.com> - 2011-06-18 10:22 -0400
  Re: What's the best way to write this base class? Ethan Furman <ethan@stoneleaf.us> - 2011-06-18 08:37 -0700
    Re: What's the best way to write this base class? John Salerno <johnjsal@gmail.com> - 2011-06-18 09:26 -0700
      Re: What's the best way to write this base class? Chris Angelico <rosuav@gmail.com> - 2011-06-19 02:34 +1000
      Re: What's the best way to write this base class? Chris Kaynor <ckaynor@zindagigames.com> - 2011-06-19 18:52 -0700
        Re: What's the best way to write this base class? John Salerno <johnjsal@gmail.com> - 2011-06-19 21:04 -0700
          Re: What's the best way to write this base class? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-06-20 00:12 -0700
          Re: What's the best way to write this base class? Mel <mwilson@the-wire.com> - 2011-06-20 07:57 -0400
            Re: What's the best way to write this base class? Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-20 12:31 -0600
          Re: What's the best way to write this base class? Terry Reedy <tjreedy@udel.edu> - 2011-06-20 13:58 -0400

csiph-web