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


Groups > comp.lang.python > #7907

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

From Mel <mwilson@the-wire.com>
Newsgroups comp.lang.python
Subject Re: What's the best way to write this base class?
Followup-To comp.lang.python
Date 2011-06-18 10:22 -0400
Organization Aioe.org NNTP Server
Message-ID <iticc1$o12$1@speranza.aioe.org> (permalink)
References <142e76c3-b304-43ef-af24-919fa6146369@c9g2000yqp.googlegroups.com>

Followups directed to: comp.lang.python

Show all headers | 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
> 
> 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.

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