Question about instance creation

Robin Barendregt r.barendregt at village.uunet.be
Mon Mar 20 21:47:00 CET 2000


Hi all,

This is my first ever post to a newsgroup, so if I'm breaking every
rule, please forgive me :)
I'm a python newbie and am trying to create instances of a class that
can have a changing base class. A code example should clarify
(questions follow after code snippet):

File class1.py contains:

from class2 import *

class Class1:
    def __init__(self, arg = "no arg given"):
        print 'Class1 init'
        print arg
        self.var1 = 'this is var1'

    def method1(self):
        print "this is method1"		
		
class Class2:
    def __init__(self):
        print 'Class2 init'
        self.var2 = 'this is var2'
		
    def method2(self):
        print "this is method2"
		
class Class3(Class1, Class2):
    def __init__(self):
        Class1.__init__(self)
        Class2.__init__(self)
        print 'Class3 init'
        c = DynamicClass(Class1, "arg given")
        print c
        self.var3 = 'this is var3'

    def method3(self):
        print "this is method3"
		
if __name__ == '__main__':
    c = Class3()

And file class2.py contains:

class DummyClass:
    def __init__(self):
        pass

BaseAlias = DummyClass

class DynamicClass:
    def __init__(self, newAlias, *args):
        global BaseAlias
        BaseAlias = newAlias
        apply(BaseAlias, args)

class DerivedClass(BaseAlias):
    def __init__(self, *args):
        apply(BaseAlias.__init__, (self,) + args)


I want the var c in Class3.__init__ to contain and instance of the
class given as 1st argument to DynamicClass. I tried using return in
the DerivedClass.__init__ but I get an exception saying I should
return None in an __init__. How can I accomplish this?

Oh, and thanks for reading :)

Robin Barendregt.



More information about the Python-list mailing list