Suppose you want C to give names for some objects of type A and B and later call some print_a and print_b methods on objects of type C to return these names?
You can get this type of behavior using the C ++ inheritance model, but the python model is very different. Only one object with one set of fields. If you want C ++ behavior, the easiest way is probably to declare subobjects (and this is similar to the usual abuse of inheritance over a composition).
It looks like you are trying to do something like:
class Printable(object):
def __init__(self, name):
self.name = name
def myprint(self):
print self.name
class C(object):
def __init__(self):
self.a = Printable('A')
self.b = Printable('B')
def print_a(self):
self.a.myprint()
def print_b(self):
self.a.myprint()
if __name__ == '__main__':
c = C()
c.print_a()
source
share