0

I have a class A and a class B derived from A.

After creating an instance of class A with many operations performed, I now want to serialize that specific object. Let's call that object A_instance.

When initializing class B, how can I tell B that it's base object should be A_instance?

Within B's init i want to decide whether it should normally execute super().__init__(...) or setting the base object directly to A_instance.

Here is a code example which makes my question hopefully clear:

class A():
  def __init__(self, a=1):
    self.a = a
    self.message = "Hello, I'm class A"


myA = A(15)


class B(A):
  def __init__(self, b=2, my_base=None):
    if my_base:
      # what should i code here? maybe someting like super().super_object = my_base
      pass
    else:
      super(B, self).__init__()

    self.b = b
    self.message = "Hello, I'm class B inherited from A"


#Then the above code should result in something like:
myB = B(my_base=myA)
assert myB.a == myA.a

A similar if not even the same question for C++ can be found here: set the base object of derived object?

TylerH
  • 20,799
  • 66
  • 75
  • 101
fmfreeze
  • 197
  • 1
  • 11

0 Answers0