is this the right way to do subclasses?

Farshid Lashkari no at spam.com
Wed Nov 8 14:07:29 EST 2006


John Salerno wrote:
> Ok, back to my so-called "game." I'm just curious if I've implemented 
> the subclasses properly, because it seems like an awful lot of 
> repetition with the parameters. And again, if I want to add a new 
> attribute later, I'd have to change a lot of things. I can't help but 
> get the feeling that I'm doing something very inefficiently.

Just accept variable arguments in the constructor of the sub-classes and 
forward them to the base class.

class Fighter(Character):

     def __init__(self, *args, **kw):
         Character.__init__(self, *args, **kw)
         self.health += 2
         self.strength += 1

This way, if you add a new parameter to the base class, you won't need 
to update all the derived classes.

-Farshid



More information about the Python-list mailing list