Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'attributes': 0.05; 'instance': 0.05; 'instance,': 0.05; 'character,': 0.07; 'defaults': 0.07; 'json': 0.07; 'attribute': 0.09; 'back-end': 0.09; 'closed,': 0.09; 'subclasses': 0.09; 'pm,': 0.10; 'syntax': 0.11; 'wrote:': 0.14; '(ie,': 0.16; 'file).': 0.16; 'possibly': 0.16; '(i.e.': 0.17; 'classes,': 0.19; 'once,': 0.19; 'guess': 0.19; '(which': 0.20; 'header:In-Reply-To:1': 0.21; "aren't": 0.22; 'file,': 0.22; 'stuff': 0.22; 'code': 0.24; 'handles': 0.26; 'xml': 0.26; 'classes': 0.26; 'tried': 0.27; 'example': 0.27; "i'm": 0.27; '---': 0.28; 'message-id:@mail.gmail.com': 0.28; 'character': 0.29; 'subject:?': 0.29; 'class.': 0.29; "python's": 0.29; 'class': 0.29; 'code,': 0.29; 'config': 0.30; 'sun,': 0.30; 'developers': 0.30; 'player': 0.31; "skip:' 10": 0.32; 'file.': 0.32; 'to:addr:python-list': 0.33; 'list': 0.33; 'actually': 0.33; '(for': 0.33; "i've": 0.33; 'rather': 0.34; '...': 0.34; 'chris': 0.34; 'file': 0.34; 'there': 0.35; 'driven': 0.35; 'subject:What': 0.35; 'store': 0.35; 'using': 0.35; 'determine': 0.36; 'open': 0.36; 'probably': 0.36; 'uses': 0.36; 'difference': 0.37; 'similar': 0.37; 'received:google.com': 0.37; 'something': 0.37; 'change': 0.37; 'pretty': 0.37; 'received:74.125': 0.38; 'but': 0.38; 'data': 0.38; 'subject:: ': 0.38; 'doing': 0.39; 'should': 0.39; 'add': 0.39; 'to:addr:python.org': 0.39; 'basic': 0.40; 'read,': 0.40; 'really': 0.40; 'more': 0.60; 'your': 0.60; 'order': 0.62; 'john': 0.62; 'opened': 0.63; 'making': 0.67; 'subject:this': 0.76; 'data-driven': 0.84; 'ini': 0.84; 'read/write': 0.84; 'subject:class': 0.84; 'subject:write': 0.84; 'subject:best': 0.93 MIME-Version: 1.0 In-Reply-To: <5c8be025-2d2c-42fc-a764-bd1ca03ba398@d14g2000yqb.googlegroups.com> References: <142e76c3-b304-43ef-af24-919fa6146369@c9g2000yqp.googlegroups.com> <5c8be025-2d2c-42fc-a764-bd1ca03ba398@d14g2000yqb.googlegroups.com> Date: Mon, 20 Jun 2011 00:12:43 -0700 Subject: Re: What's the best way to write this base class? From: Benjamin Kaplan To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Junkmail-Status: score=10/49, host=mpv1.tis.cwru.edu X-Junkmail-Signature-Raw: score=unknown, refid=str=0001.0A02020A.4DFEF2EC.0071,ss=1,fgs=0, ip=74.125.83.182, so=2010-12-23 16:51:53, dmn=2009-09-10 00:05:08, mode=single engine X-Junkmail-IWF: false X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 53 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308554162 news.xs4all.nl 49042 [::ffff:82.94.164.166]:34124 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8006 On Sun, Jun 19, 2011 at 9:04 PM, John Salerno wrote: > On Jun 19, 8:52=A0pm, Chris Kaynor wrote: > >> Having a character class (along with possibly player character, non-play= er character, etc), make sense; however you probably want to make stuff lik= e health, resources, damage, and any other attributes not be handles by any= classes or inheritance in order to allow you to make such data-driven (ie,= read from a file). Doing so makes the game much more extendable: using cla= sses, you are likely limited to 5 or 'combinations and a few developers (pl= us, any designers need to know programming). >> >> A basic way to determine between using subclasses over a data driven app= roach is: is there significantly different back-end behavior or merely attr= ibute differences. > > Can you give a basic example of how this data-driven approach would > work? You don't have to provide any code, just a description would be > helpful. Such as, do I create a data file per character, and then have > each character instance read/write to that file? Is it good to have so > many files open at once, or would they only need to be read, closed, > then opened again at the end to write? > -- I'm pretty sure he means that if the only difference between classes is configuration (i.e. you aren't actually going to change code between character classes, just base stats, growth rates, and a list of available skills or something of that nature), then you should store the configurations in a config file rather than making a new class. So rather than having class WizardCharacter(Character) : base_health =3D 50 ... class WarriorCharacter(Character) : base_health=3D70 ... You make a config file --- characterclasses.ini --- [Wizard] base_health=3D50 [Warrior] base_health=3D70 Then, when you make a new character, rather than doing a WizardCharacter() or a WarriorCharacter(), you do a Character(job=3D'Wizard') and then look up the various defaults in your config file. Doing it this way makes it trivial to add a new class. If you want to use an old-fashioned INI file, you can use the ConfigParser class to read them. If you want to nest attributes (for instance, a list of sub-items), you'll probably want to go with XML and ElementTree. I guess you can also use JSON (which uses a syntax similar to Python's dictionaries) but I've never really tried to make one of those by hand before so I'm not sure how well it will work out.