What's the best way to write this base class?
bruno.desthuilliers at gmail.com
bruno.desthuilliers at gmail.com
Sat Jun 18 06:55:37 EDT 2011
On 18 juin, 06:17, John Salerno <johnj... at gmail.com> wrote:
> Note: I have in mind that when a specific subclass (Warrior, Wizard,
> etc.) is created, the only argument that will ever be passed to the
> __init__ method is the name. The other variables will never be
> explicitly passed, but will be set during initialization.
__init__ is actually supposed to be the initialization phase, but well
<g>
> 1)
> class Character:
If you using Python 2.x, make this:
class Character(object):
> def __init__(self, name, base_health=50, base_resource=10):
> self.name = name
> self.health = base_health
> self.resource = base_resource
If neither base_health nor base_resource are supposed to be passed in,
why make them arguments at all:
class Character(object):
def __init__(self, name):
self.name = name
self.health = 50
self.resource = 10
> 2)
> class Character:
>
> base_health = 50
> base_resource = 10
>
> def __init__(self, name):
> self.name = name
> self.health = base_health
> self.resource = base_resource
Did you at least tried this one ? Hint: it won't work.
> 3)
> BASE_HEALTH = 50
> BASE_RESOURCE = 10
>
> class Character:
>
> def __init__(self, name):
> self.name = name
> self.health = BASE_HEALTH
> self.resource = BASE_RESOURCE
This is probably what I'd do.
More information about the Python-list
mailing list