Inheritance question

Yannick Turgeon nobody at nowhere.com
Mon Sep 20 09:46:03 EDT 2004


Hello,

Say I have a base class called "A"

class A:
    _value
    __init__(self, value):
        self._value = value

and two derived classes "B1" and "B2":

class B1(A):
    def doPrint(self):
        print "B1: " + str(self._value)

class B2(A):
    def doPrint(self):
        print "B2: " + str(self._value)


is there a way that I could change my class A to something like:

class A:
    _value
    __init__(self, value):
        self._value = value
        if value > 10:
            set myself as an instance of B1   # How can I do that?
        else:
            set myself as an instance of B2

in the real situation "value" is encoded and the way to know what kind of A
it is, is more complex than just look at the value.

Thanks for your help and time.

Yannick





More information about the Python-list mailing list