Class inheritance testing I'm just messing around with class inheritance. I originally had a separate goal in mind but I ended up here, and my new goal was to have a parent that was completely ignorant of it's self, i.e. no default attributes. This would seem to make it easier to subclass, the MeleClass could set; for instance canRange to True during instantiation without modifying the parent's parameters.

Questions:


Are there any other ways to do this?
How could this be bad?
Are there more efficient ways?




class Character:
    def __init__(self, **kwargs):
        for i in kwargs:
            setattr(self, i, kwargs[i])

class MeleClass(Character):
    def __init__(self, name="default", atk=1, deff=1):
        self.canRange = False
        Character.__init__(self, name=name, attack=atk, defence=deff)

class SpellClass(Character):
    def __init__(self, name="default", atk=1, deff=1):
        self.canRange = True
        Character.__init__(self, name=name, attack=atk, defence=deff)


p = MeleClass(name='TheLazyScripter')
print p.name
print p.attack
print p.canRange
I would change your Character.__init__ to this:

def __init__(self, **kwargs):
    for key, value in kwargs.iteritems():
        setattr(self, key, value)


It isn't any shorter but it's more clear what's happening.
For one thing, i is not a descriptive name by any means.  For another thing, you might as well iterate through key-value pairs1 rather than just keys.  You use both; why not iterate through both?

I would also change your other __init__ methods.  If Character.__init__ sets attributes, why not take advantage of it?  Instead of self.canRange = ..., add canRange=... to the arguments to Character.__init__.

That brings me to something else.  PEP 8, the Python style guide, says this about instance variables (such as canRange):


  Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.





1 You could also do self.__dict__.update(kwargs) but that isn't recommended and seems hackish to me anyway.