0

I am trying to have multiple classes inherit from one class but use the same instance and not create a different instance each time.

class ClassA(object):
    def __init__(self):
        self.process_state = "Running"
        self.socket_list = []
    def method_a(self):
        print("This is method a")
        

class ClassB(ClassA):
    def __init__(self):
        super().__init__()
        
    def method_b(self):
        if self.process_state == "Running":
            self.socket_list.append("From Method B")
            print(self.socket_list)
            print("This is method b")


class ClassC(ClassA):
    def __init__(self):
        super().__init__()
        
    def method_c(self):
        if self.process_state == "Running":
            print(self.socket_list)
            self.socket_list.append("From Method C")
            print("This is method c")   
            print(self.socket_list)

   

Functions ran:

CB = ClassB() 
CB.method_b() 
CB.method_b()

CC = ClassC() 
CC.method_c()

Result:

['From Method B']
This is method b
['From Method B', 'From Method B']
This is method b
[]
This is method c
['From Method C']

Desired result:

['From Method B']
This is method b
['From Method B', 'From Method B']
This is method b
['From Method B', 'From Method B']
This is method c
['From Method B', 'From Method B', 'From Method C']

I am trying to have multiple class inherit from one class but use the same instance and not create a different instance each time.

martineau
  • 119,623
  • 25
  • 170
  • 301
John
  • 47
  • 2
  • Define `socket_list` as class variable of `ClassA`. Read [Class and Instance Variables¶](https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables). – Olvin Roght Jan 20 '22 at 21:37
  • Does this answer your question? [Inheriting from instance in Python](https://stackoverflow.com/questions/1081253/inheriting-from-instance-in-python) – TheFungusAmongUs Jan 20 '22 at 21:38
  • You are doing the `super()._init_` wrong. Do `ClassA._init_(self)` – Verthais Jan 20 '22 at 21:39
  • 2
    @Verthais, it's not just bad, it's harmful suggestion. `super().__init__(self)` is an appropriate way. – Olvin Roght Jan 20 '22 at 21:40
  • 2
    @Verthais That is not correct. `super().__init__` is the correct way to call a superclass's `__init__`. Read more: https://stackoverflow.com/questions/222877/what-does-super-do-in-python-difference-between-super-init-and-expl – TheFungusAmongUs Jan 20 '22 at 21:43
  • But you can't use super with multiple inheritance, can you? – Verthais Jan 20 '22 at 21:43
  • @Verthais you absolutely can, indeed, that is the *main feature* of `super`, that it allows cooperative multiple inheritance. Read this blog post from a Python core developer: https://rhettinger.wordpress.com/2011/05/26/super-considered-super/ – juanpa.arrivillaga Jan 20 '22 at 22:04

1 Answers1

0

One way you can achieve this is by making the socket list a static variable, though I'm not sure if this is recommended.

class ClassA(object):
    socket_list = []
    def __init__(self):
        self.process_state = "Running"
    def method_a(self):
        print("This is method a")

Output:

['From Method B']
This is method b
['From Method B', 'From Method B']
This is method b
['From Method B', 'From Method B']
This is method c
['From Method B', 'From Method B', 'From Method C']
Richard K Yu
  • 2,152
  • 3
  • 8
  • 21
  • 2
    I'm wondering, make screenshot is more complicated than just copy data, but I always see screenshots which is not recommended. – Olvin Roght Jan 20 '22 at 21:39
  • @OlvinRoght This is a good point, I usually include screenshot only in cases where I want to prove that I ran a program and it worked (but also in this case since am not providing copy and pastable code to run). – Richard K Yu Jan 20 '22 at 21:40