Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Mel Newsgroups: comp.lang.python Subject: Re: What's the best way to write this base class? Followup-To: comp.lang.python Date: Sat, 18 Jun 2011 10:22:59 -0400 Organization: Aioe.org NNTP Server Lines: 46 Message-ID: References: <142e76c3-b304-43ef-af24-919fa6146369@c9g2000yqp.googlegroups.com> Reply-To: mwilson@the-wire.com NNTP-Posting-Host: 3BXkqO+E3M8bvjcjoKzVnA.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Complaints-To: abuse@aioe.org User-Agent: KNode/4.4.8 X-Notice: Filtered by postfilter v. 0.8.2 Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7907 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 > > 2) > class Character: > base_health = 50 > base_resource = 10 > def __init__(self, name): > self.name = name > self.health = base_health > self.resource = base_resource > > 3) > BASE_HEALTH = 50 > BASE_RESOURCE = 10 > class Character: > def __init__(self, name): > self.name = name > self.health = BASE_HEALTH > self.resource = BASE_RESOURCE For completeness, there's also 4) Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class Character (object): ... health = 50 ... def __init__ (self, name): ... self.name = name ... print self.name, self.health ... >>> Character ('Eunice') Eunice 50 where the class attribute is used until it's overridden in the instance. Mel.