I need your advice guys please, as I'm pretty new to programming and especially OOP. What I need is to get seperately all instances of every subclass and also all of them together. I just can't figure out. Thank you very much! - Class B there i wanted to use as a Loggable function, but maybe it's just nonsense.
class A:
def __init__(self, name):
self.__name = name
def __str__(self):
return f'\n\tName: {self.__name}'
class Loggable(A):
instances = []
def __init__(self):
self.__class__.instances.append(self)
class C(Loggable):
def __init__(self, name, x1, x2):
super(Loggable, self).__init__(name)
super().__init__()
self.__x1 = x1
self.__x2 = x2
def __str__(self):
return f'{super().__str__()}\n\tX1: {self.__x1}\n\tX2: {self.__x2}'
class D(Loggable):
def __init__(self, name, g1, g2):
super(Loggable, self).__init__(name)
super().__init__()
self.__g1 = g1
self.__g2 = g2
def __str__(self):
return f'{super().__str__()}\n\tG1: {self.__g1}\n\tG2: {self.__g2}'
class TableDump(C, D):
@classmethod
def dumpC(cls):
for instance in cls.instances:
print(instance)
@classmethod
def dumpD(cls):
for instance in cls.instances:
print(instance)
def main():
c1 = C("C1" , "C1", "C1")
c2 = C("C2" , "X1", "X2")
c3 = C("C3" , "X1", "X2")
d1 = D("D1" , "D1", "D1")
d2 = D("D2" , "D2", "D2")
d3 = D("D3" , "D3", "D3")
TableDump.dumpC()
if __name__ == '__main__':
main()