1

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()


MH99
  • 15
  • 5
  • 1
    There are no instances created of any of the classes in the code posted in your question. – martineau Dec 05 '20 at 23:05
  • I know, suppose they are 2 instances of C and 2 of D. What I need is to get them separately and also together. – MH99 Dec 05 '20 at 23:29
  • You missed my point. Please provide a [mre] and show the results plus what the desired results would have been. – martineau Dec 05 '20 at 23:49
  • Okay, I've tried to wrote it below. – MH99 Dec 06 '20 at 01:21
  • Sorry, a [mre] is code that others can run and will reproduce the problem. Posting the desired results I would think is self-explanatory — you've done neither so far. – martineau Dec 06 '20 at 01:23
  • Sorry.. what about now the code above? Does it work? If not, then I'm lost. – MH99 Dec 06 '20 at 01:36
  • Apologies, it appears you have now supplied a MRE — just not an example of the desired results. What's wrong with the current results? – martineau Dec 06 '20 at 02:25
  • martineau: Now its printing all instances together, how to print for example instances just of subclass D? Gino: Im going to look at it. – MH99 Dec 06 '20 at 12:51

2 Answers2

0

Sorry rewriting, but I do this for simplicity:

class A:
    instances = []
    
    def __init__(self):
        self.instances.append(self)

class B(A):
    pass

Now you have access to all instances of class A and all subclasses via list A.instances

Glech
  • 731
  • 3
  • 14
0

OK, from your comment describing what's wrong with your current code, I think I finally understand what you want to know how to do. One approach would be to keep a list of all instances, and then filter all those of a certain type.

Below is an illustration on a couple of ways of doing that based on the code in your question. Note that I removed the Loggable class you had because it wasn't strictly necessary — but it could be put back in if it is needed for some reason.

class A:
    instances = []

    def __init__(self, name):
        self.__name = name
        self.__class__.instances.append(self)

    def __str__(self):
        return f'\n\tName: {self.__name}'


class C(A):
    def __init__(self, name, x1, x2):
        super().__init__(name)
        self.__x1 = x1
        self.__x2 = x2

    def __str__(self):
        return (f'{super().__str__()}\n'
                f'\tX1: {self.__x1}\n\tX2: {self.__x2}')

class D(A):
    def __init__(self, name, g1, g2):
        super().__init__(name)
        self.__g1 = g1
        self.__g2 = g2

    def __str__(self):
        return (f'{super().__str__()}\n'
                f'\tG1: {self.__g1}\n\tG2: {self.__g2}')


class TableDump(A):
    @classmethod
    def dump_instances(cls, Kind):
        """ Generic instance dumper. """
        for instance in cls.instances:
            if type(instance) is Kind:
                print(instance)

    @classmethod
    def dumpC(cls):
        for instance in cls.instances:
            if type(instance) is C:
                print(instance)

    @classmethod
    def dumpD(cls):
        for instance in cls.instances:
            print(instance)


def main():
    c1 = C("C_name1", "C1", "C1")
    c2 = C("C_name2", "X1", "X2")
    c3 = C("C_name3", "X1", "X2")

    d1 = D("D_name4", "D1", "D1")
    d2 = D("D_name5", "D2", "D2")
    d3 = D("D_name6", "D3", "D3")

#    TableDump.dumpC()
    TableDump.dump_instances(C)  # Same results as previous line.


if __name__ == '__main__':
    main()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Thanks a lot sir! This is what I meant, but I just couldn't express myself very well. – MH99 Dec 06 '20 at 18:57