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.

7
  • Define socket_list as class variable of ClassA. Read Class and Instance Variables¶. Commented Jan 20, 2022 at 21:37
  • Does this answer your question? Inheriting from instance in Python Commented Jan 20, 2022 at 21:38
  • You are doing the super()._init_ wrong. Do ClassA._init_(self) Commented Jan 20, 2022 at 21:39
  • 2
    @Verthais, it's not just bad, it's harmful suggestion. super().__init__(self) is an appropriate way. Commented Jan 20, 2022 at 21:40
  • 2
    @Verthais That is not correct. super().__init__ is the correct way to call a superclass's __init__. Read more: stackoverflow.com/questions/222877/… Commented Jan 20, 2022 at 21:43

1 Answer 1

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']
Sign up to request clarification or add additional context in comments.

2 Comments

I'm wondering, make screenshot is more complicated than just copy data, but I always see screenshots which is not recommended.
@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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.