0

My question is a bit more complicated as the question described here.

So assuming we have two Superclasses, how can i istantiate the subclass depending on the superclass i passed?

class ASuperClass:
    def __init__(self, tediously, many, attributes):
        # assign the attributes like "self.attr = attr"

class BSuperClass:
    def __init__(self, tediously, many, attributes):
        # assign the attributes like "self.attr = attr"

What i tried is:

class SubClass(ASuperClass, BSuperClass):
    def __init__(self, id, a_super_class_instance, b_super_class_instance):
        self.id = id
        if a_super_class_instance:
           ASuperClass().__init__(**a_super_class_instance)
        elif:
           ASuperClass().__init__(**b_super_class_instance)
        else: 
           raise ValueError("At least one of the superclasses have to be passed.")

However, this did not work. My wish is depending on the input, i get the subclass with the params as output.

s1 = SubClass(id, ASubClass)
s2 = SubClass(id, BSubClass)
ChrisDelClea
  • 307
  • 2
  • 8

1 Answers1

0

Actually its farm simpler than i expected, this is the solution:

class SubClass(ASuperClass, BSuperClass):
    def __init__(self, id, asubclass=None, bsubclass=None):
        self.id = id
        if asubclass:
           ASuperClass().__init__(self, **(asubclass.__dict__))
        elif bsubclass:
           BSuperClass().__init__(self, **(bsubclass.__dict__))
        else: 
           raise ValueError("At least one of the superclasses have to be passed.")

s1 = SubClass(id=0, asubclass=a1)
s2 = SubClass(id=1, bsubclass=b1)

ChrisDelClea
  • 307
  • 2
  • 8