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

John Salerno johnjsal at gmail.com
Sat Jun 18 00:17:42 EDT 2011


Let's say I'm writing a game (really I'm just practicing OOP) and I
want to create a "Character" base class, which more specific classes
will subclass, such as Warrior, Wizard, etc. Which of the following
ways is better, or is there another way?

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. With that in
mind, here are the ways I've come up with:

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



More information about the Python-list mailing list