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


Groups > comp.lang.python > #7912

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'assign': 0.04; 'instance': 0.05; 'character,': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:gator410.hostgator.com': 0.09; 'to:name:python list': 0.09; '~ethan~': 0.09; 'def': 0.12; 'wrote:': 0.14; 'character:': 0.16; 'creation.': 0.16; 'made,': 0.16; 'name):': 0.16; 'class,': 0.16; 'to:2**1': 0.20; 'header:In-Reply-To:1': 0.21; 'fine': 0.22; 'etc,': 0.23; 'specify': 0.25; 'subject:?': 0.29; 'explicitly': 0.29; 'class': 0.29; 'yet': 0.32; 'to:addr:python-list': 0.33; 'characters': 0.34; 'option': 0.35; 'header:User-Agent:1': 0.35; 'subject:What': 0.35; 'charset:us-ascii': 0.36; 'assigned': 0.37; 'change': 0.37; 'subject:: ': 0.38; 'said': 0.39; 'to:addr:python.org': 0.39; 'john': 0.62; 'here': 0.66; 'received:websitewelcome.com': 0.67; 'received:69.93': 0.67; 'route': 0.67; 'subject:this': 0.76; 'subject:class': 0.84; 'subject:write': 0.84; 'subject:best': 0.93; 'received:gateway02.websitewelcome.com': 0.95
Date Sat, 18 Jun 2011 08:37:00 -0700
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Thunderbird 2.0.0.24 (Windows/20100228)
MIME-Version 1.0
To John Salerno <johnjsal@gmail.com>, python list <python-list@python.org>
Subject Re: What's the best way to write this base class?
References <142e76c3-b304-43ef-af24-919fa6146369@c9g2000yqp.googlegroups.com>
In-Reply-To <142e76c3-b304-43ef-af24-919fa6146369@c9g2000yqp.googlegroups.com>
Content-Type text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - gator410.hostgator.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - stoneleaf.us
X-BWhitelist no
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender c-67-170-168-84.hsd1.or.comcast.net ([192.168.74.5]) [67.170.168.84]:1474
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 1
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ==
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.122.1308411487.1164.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 82.94.164.166
X-Trace 1308411487 news.xs4all.nl 49177 [::ffff:82.94.164.166]:33785
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7912

Show key headers only | View raw


John Salerno wrote:
> 1)
> class Character:
> 
>     def __init__(self, name, base_health=50, base_resource=10):
>         self.name = name
>         self.health = base_health
>         self.resource = base_resource

You said above that health and resource will never be explicitly passed, 
yet here you have allowed for that possibility.  If you are going to 
have mosters, etc, also inherit from Character, with different health 
and resources, I would go this route with one change:

     def __init__(self, name, base_health, base_resoures):

and always specify those numbers on creation.


> 2)
> class Character:
> 
>     base_health = 50
>     base_resource = 10
> 
>     def __init__(self, name):
>         self.name = name
>         self.health = base_health
>         self.resource = base_resource

You do not need to assign health and resource here -- they are already 
assigned on the class, so the instance will see them automatically. 
When a change is made, the instance will automagically get its own copy.


> 3)
> BASE_HEALTH = 50
> BASE_RESOURCE = 10
> 
> class Character:
> 
>     def __init__(self, name):
>         self.name = name
>         self.health = BASE_HEALTH
>         self.resource = BASE_RESOURCE

If *all* characters (player, non-player, monster, etc) will have the 
same base health and resources then this is fine -- otherwise I would 
use option 1.

~Ethan~

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