If what you're trying to do is have more control over the type of
object that is instantiated, then you could use a function that decides
what class to use based upon the arguments supplied to the function,
where it then instantiates an object from the chosen class, then
returns the object. The __init__ method is just for initialization when
an object is created, it's not a constructor and you could even leave
it out and still be able to create objects (although that's less useful
in most cases). Changing an object's type after it's already been
created is more advanced. It's useful, but I don't think that's what
you're trying to do. I believe you're just trying to allow the program
to have more control over what type of object to create. Try doing a
Google search for "creational design patterns", "factory function", and
"factory method". Here's a simple example of what I'm talking about:

def factory(chosen):
    if chosen == 'a':
        obj = typeA()
    elif chosen == 'b':
        obj = typeB()

    return obj


KraftDiner wrote:
> This is not working the way I think it should....
> it would appear that fromfile and getName are calling the baseClass
> methods which are
> simple passes.... What have I done wrong?
>
> class baseClass:
>       def __init__(self, type):
>               if type == 'A':
>                       self = typeA()
>               else:
>                       self = typeB()
>       def fromfile(self):
>               pass
>       def getName(self):
>               pass
>
> class typeA(baseClass):
>       def __init__(self):
>               self.name='A'
>               print 'typeA init'
>       def fromfile(self):
>               print 'typeA fromfile'
>       def getName(self):
>               print self.name
>
> class typeB(baseClass):
>       def __init__(self):
>               self.name='B'
>               print 'typeB init'
>       def fromfile(self):
>               print 'typeB fromfile'
>       def getName(self):
>               print self.name
>
> a = baseClass('A')
> a.fromfile()
> a.getName()
>
> b = baseClass('B')
> b.fromfile()
> b.getName()
> 
> log:
> typeA init
> typeB init

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to