is this the right way to do subclasses?

John Salerno johnjsal at NOSPAMgmail.com
Wed Nov 8 14:33:01 EST 2006


Peter Otten wrote:

> One way to avoid the repetition:
> 
> class Character(object):
>     def __init__(self, name, strength, dexterity, intelligence):
>         self.name = name
>         self.health = 10
>         self.strength = strength
>         self.dexterity = dexterity
>         self.intelligence = intelligence
>         self.fix_attributes()
> 
> class Fighter(Character):
>     def fix_attributes(self):
>         self.health += 2
>         self.strength += 1
> 
> You may need a no-op implementation of fix_attributes() in the Character
> class.
> 
> Peter

But how does this take care of the initialization of the stats? I won't 
be able to use Fighter to create a character without doing everything in 
Character, right?



More information about the Python-list mailing list