1

Suppose I have two classes:

class A():
    pass

class B():
    pass

I have another class

class C(object):
    def __init__(self, cond):
        if cond ==True:
           # class C initialize with class A
        else:
           # class C initialize with class B

If I inherit from A or B, by this implementation is it possible?

3
  • 2
    What are you trying to achieve? The way you have written your code, class C has no relationship to either A or B. Commented Jun 3, 2011 at 16:27
  • 1
    You've also forgotten two underscores; it should be __init__. Commented Jun 3, 2011 at 16:28
  • 1
    that is i want to write one class as wrapper of two other class... and want to access object of these two class by a single class . Commented Jun 3, 2011 at 16:30

4 Answers 4

4

If you want to set the class use the __class__ variable.

class C(object):
    def __init__(self, cond):
        if cond ==True:
           self.__class__ = A
        else:
           self.__class__ = B
        self.__class__.__init__(self)
Sign up to request clarification or add additional context in comments.

1 Comment

Ugh! No! Gross! Don't do this!
3

Since you didn't give a very good example why that could ever be useful I'll just assume that you didn't understand OOP.

What you're trying to do might be some kind of factory pattern:

def something_useful(cond):
    if cond:
        return A()
    else:
        return B()

myobj = something_useful(cond)

or maybe you want aggregation:

class C(object):
    def __init__(self, something_useful):
        # store something_useful because you want to use it later
        self.something = something_useful

# let C use either A or B - then A and B really should inherit from a common base
if cond:
    myobj = C(A())
else:
    myobj = C(B())

1 Comment

Yes, you can just rename something_useful to C and it's a drop-in solution.
1

Do you mean you want to do some sort of mix-in depending on the value of cond?

If so try

class C(object):
    def __init(self, cond):
        if cond ==True:
           self.__bases__ += A
        else:
           self.__bases__ += B

I'm not 100% sure this is possible since perhaps it only works C.bases += A. If it's not possible then what you are trying to do is probably not possible. C should either inherit from A or from B.

1 Comment

AttributeError: 'C' object has no attribute 'bases' is nor working
1

While I'll not be as severe as Jochen, I will say that you are likely taking the wrong approach. Even if it is possible, you're far better off using multiple inheritance and having an AC and a BC class.

Eg:

class A():
    pass

class B():
    pass

class C():
    #do something unique which makes this a C
    pass

#I believe that this works as is?
class AC(A,C):
    pass

class BC(B,C):
    pass

This way, you can simply call

def get_a_c(cond):
    if cond == True:
       return AC()
    return BC()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.