I have a python class that serves as a baseclass for further subclasses. It contains a methods that should act on all subclass the same, e.g. I want to put it in the base class. The problem is, that this method should return a new instance of the subclass. But I since the baseclass is located before the definition of the subclass, I can not create a new instance of the subclass, as it is not known in the scope of the baseclass:
class Base:
def __init__(self, value):
self.value = value
def convert_child_classes(self, newclass):
newvalue = MakeUsableInNewclass(self.value)
newattr = MakeUsableInNewclass(self.subclassattr)
return newclass(newattr, newvalue)
class Child1(Base):
def __init__(self, subclassattr, value)
super(Child, self).__init__(value)
self.subclassattr = subclassattr
def MethodForSubClassAttr(self):
...do sth with self.subclassattr...
class Child2(Base):
def __init__(self, subclassattr, value)
super(Child, self).__init__(value)
self.subclassattr = subclassattr
def SomeOtherSubClassAttrMethod(self):
...do sth that is related to this class attr...
I if I have an instance of Child1 I want to be able to do some stuff with its data and then return an instance of the Child2 with the new values when calling convert_child_classes(Child2):
A = Child1('someattr', 5)
B = A.convert_child_classes(Child2)
now B should be an instance of Child2 with a value that was calculated form Child1. But since Base class does now know what Child1 or Child2 is, it can not initiate the new class.