0

So, my question is, if I were to make a Race based on this class, say, an elf. I make the elf like this: elf = Race('Elf', 100, 50, 60, 90)

If the elf got into combat, how do I make his health go down in that one instance, or any of his other stats go up based on a bonus of some sort, like if he had some kind of equipment giving him +(stat)?

Kind of a specific question, I know.

Here is the class Race I have set up so far. . .

class Race(object):

race = None
health = None
strength = None
speed = None 
endurance = None


def __init__(self, race, health, strength, speed, endurance):
    self.race = race
    self.health = health
    self.strength = strength
    self.speed = speed
    self.endurance = endurance


def set_race(self, race):
    self.race = race

def set_health(self, health):
    self.health = health

def set_strength(self, strength):
    self.strength = strength

def set_speed(self, speed):
    self.speed = speed

def set_endurance(self, endurance):
    self.endurance = endurance

Criticism is welcome, so long as its constructive!

JohnDoe000
  • 11
  • 2
  • Unfortunately, this is much better suited for a one-on-one tutorial than the generalized nature of SO, since you're essentially asking about the fundamentals of object instances and how fields are modified. – Nathan Tuggy Feb 28 '15 at 04:02

1 Answers1

0

Well, for one, the code wouldn't compile due to IndentationError but that's besides the point I suppose (so, please indent with 4 SPACES, not tab as per PEP-8 for each function body and class body).

With that said, you don't need any getters or setters because it's not pythonic. If you want to change the attribute of an object, just change the attribute. If you need to control how an attribute is set, then use properties.

Also, in your code below, you are actually not setting instance variables, you are setting member variables (please keep in mind I fixed the indentation):

class Race(object):

    race = None
    health = None
    strength = None
    speed = None 
    endurance = None

If you were to remove the above attributes but kept all the self.* in the __init__() function, you would then have instance attributes. The term self.* is stating "this is an attribute of myself, of my exact instance of me".

Also, please consider renaming race. You want a class to encapsulate all the attributes of an object. Think about it: a character's race doesn't have a health. A character can have a health, but the character also has a race. Both of these things are attributes of the character. I would suggest naming this class to class Character(object):.

Community
  • 1
  • 1
the_constant
  • 681
  • 4
  • 11
  • The code I posted removed the indents I had for some reason. It doesn't look like that in my text editor. The stats are part of the race(Each has a starting stat) I'm then adding the race to the character class, so that the characters base stats are that of the race, and are then able to be modified through armor and weapon bonuses, spell effects and battlefield effects(like hills or a dense forest). The problem I am having is modifying the stats, and including the Race class into the character class. I'm starting to wonder if it would be easier to do so with a list of races and race stats. – JohnDoe000 Mar 01 '15 at 00:54