conditional __init__

King animator333 at gmail.com
Mon Nov 2 20:40:25 CET 2009


class A(object):
    def __init__(self):
        pass
    def printme(self):
        print "I am A"

class B(object):
    def __init__(self):
        pass
    def printme(self):
        print "I am B"

class K(A, B):
    def __init__(self, value=0):
        if value == 0:
            A.__init__(self)
            print "__init__ A"
        elif value == 1:
            B.__init__(self)
            print "__init__ B"
        self.printme()

o = K(value=1)

Output
>>__init__ B
>>I am A

In above code "B" is correctly getting initialized as per condition.
How ever method "printme" is printing "I am A".
Instead it has to print "I am B" because "B" is the one that has been
initialized. What's wrong here?

Is there a better/another way to do conditional initialization as
needed above?

Cheers

Prashant
Python 2.6.2
Win XP 32



More information about the Python-list mailing list