dumb q: repeated inheritance in python?

Corey Coughlin corey.coughlin at attbi.com
Thu Sep 11 23:19:15 CEST 2003

Hi, here's a silly question.  I'm wrote some code a while back that
looks like this:

import types

class Named(object):
    def __init__(self,nm=''):
        self.iname = ''
    def setnm (self,nm):
        if isinstance(nm, types.StringType):
            self.iname = nm
            raise TypeError, "Name passed to Named isn't a string."
    def getnm (self):
        return self.iname
    name = property(getnm,setnm)

Pretty basic, a name attribute with simple type checking, it works
fine and the code using it is fine.  Well, it was, until I came up
with an object that needed two names, with the same basic type
checking.  Now the simplistic way to do that would be copy this class
to another class, say Named2, rename the property name2 and be done
with it.  On the other hand, having code that's basically an exact
duplicate of other code except for some name changes seems a tad
inelegant.  What I'd really like to do is inherit this same object
twice, but the second time rename the objects so that I get 'name2'
instead of 'name'.  (And somehow rename the functions, and so on.)

But I can't figure out how to do it.  Is there some way to do this?  I
suspect there might be some way to do it with metaclasses, but I
haven't really wrapped my head around that stuff yet.  Maybe something
with the new 'super' keyword?  Beats me.  Anyway, any help would be
great, thanks!!!

More information about the Python-list mailing list