On Wed, Aug 16, 2006 at 11:07:08AM -0700, KraftDiner wrote: [...] > 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. > 2) getName is doing nothing... > > class baseClass: > def __init__(self): > pass > def fromfile(self, str): > if (str == 'A'): > a = typeA() > else: > a = typeB() > def getName(self): > pass > > class typeA(baseClass): > def __init__(self): > self.name='A' > print 'typeA init' > def fromfile(self, str=None): > print 'typeA fromfile' > def getName(self): > print self.name > > class typeB(baseClass): > def __init__(self): > self.name='B' > print 'typeB init' > def fromfile(self, str=None): > print 'typeB fromfile' > def getName(self): > print self.name > > bc = baseClass() > bc.fromfile('A') > bc.getName() > bc.fromfile('B') > bc.getName() > bc.getName() > > log: > typeA init > typeB init >
I didn't follow up this thread from the begining, but I think this is what you want; Python 2.4.3 (#1, Mar 31 2006, 09:09:53) [GCC 2.95.3 20010315 (release)] on sunos5 Type "help", "copyright", "credits" or "license" for more information. >>> class baseClass: ... @staticmethod ... def fromfile(string): ... if string == "A": ... return typeA() ... else: ... return typeB() ... def getName(self): ... print self.name ... >>> class typeA(baseClass): ... def __init__(self): ... self.name = "A" ... print "typeA init" ... >>> class typeB(baseClass): ... def __init__(self): ... self.name = "B" ... print "typeB init" ... >>> a = baseClass.fromfile("A") typeA init >>> a.getName() A >>> b = baseClass.fromfile("B") typeB init >>> b.getName() >>> B >>> -- Inyeol Lee -- http://mail.python.org/mailman/listinfo/python-list