KraftDiner a écrit : (snip) > > Here I tried this example and maybe this will explain the difficulties > I'm having. > 1) at the time the baseClass is constructed shouldn't the constructor > of the appropriate type be called.
It is. But neither the constructor nor 'the appropriate type' are what you think they are. > 2) getName is doing nothing... Of course. It's body is a 'pass' statement, which in Python means 'do nothing'. > class baseClass: > def __init__(self): > pass This initializer is useless. > def fromfile(self, str): > if (str == 'A'): > a = typeA() > else: > a = typeB() And then the method returns and the object bound to the local name 'a' is garbage-collected. > def getName(self): > pass > > class typeA(baseClass): > def __init__(self): > self.name='A' > print 'typeA init' > def fromfile(self, str=None): > print 'typeA fromfile' This method is not called by your code > def getName(self): > print self.name This method is not called by your code > class typeB(baseClass): > def __init__(self): > self.name='B' > print 'typeB init' > def fromfile(self, str=None): > print 'typeB fromfile' This method is not called by your code > def getName(self): > print self.name This method is not called by your code > bc = baseClass() creates an instance of baseClass and bind it to the name bc > bc.fromfile('A') calls the fromfile method of class baseClass with (object bound to) bc as first param and 'A' as second param. This methods creates an instance of typeA, bind it to the local name 'a', and then returns. > bc.getName() calls the getName method of class baseClass, which body is a 'pass' statement. > bc.fromfile('B') calls the fromfile method of class baseClass with (object bound to) bc as first param and 'B' as second param. This methods creates an instance of typeB, bind it to the local name 'a', and then returns. > bc.getName() calls the getName method of class baseClass, which body is a 'pass' statement. > bc.getName() calls the getName method of class baseClass, which body is a 'pass' statement. See John Henry's answer for a correct implementation. -- http://mail.python.org/mailman/listinfo/python-list