0

I have a class called resources and I have defined one method called get_connect. I want to use the data of which get_connect returns to the other classes. I need at least three classes and I use the data of get_connect and I have to parse that data. To implement this I have written the code below

 class resources: @staticmethod def get_connect(): return 1 + 2 class Source1(resources): def __init__(self): self.response = resources.get_connect() def get__details1(self): print(self.response) class Source2(resources): def __init__(self): self.response = resources.get_connect() def get_details2(self): print(self.response) class Source3(resources): def __init__(self): self.response = resources.get_connect() def get__detail3(self): print(self.response) source1 = Source1() source2 = Source2() source3 = Source3() source1.get__details1() source2.get_details2() source3.get__detail3() 

But the problem with the code is for every class in init method I am calling the get_connect method. I don't want to repeat the code. I need help for avoiding redundancy which I have asked below

  1. Is there any way I can call get_connect in one place and use it for other classes maybe a decorator or anything? if yes how can I?
  2. While creating objects also I am calling each class and calling each method every time. is there a way to use any design pattern here?

If anyone helps me with these oops concepts it will be useful.

4
  • 1
    Why do you need three classes that all do the same and then a superclass on top? All you need is one class in total. Commented Mar 18, 2022 at 11:05
  • All these three classes will do differently in my actual scenario. That's why I have created three different classes. In this example I have shown same thing Commented Mar 18, 2022 at 11:07
  • btw, why do you inherit from resources and still use resources.get_connect? also are you sure that those classes will do completely different things that can't possibly be done by one class? so far they don't do that, especially considering that all of them get their response in the same way Commented Mar 18, 2022 at 11:08
  • Yes, It's working without inheriting the resources also. Thanks. Yes, I need three classes as per the requirements. I have to parse the response I get from get_connect. But what I need to know here is there any other better way to call the get_connect and use it in the three classes Commented Mar 18, 2022 at 11:17

1 Answer 1

1

First of all, is there any reason why you are using get_connect method as static?

Because what you can do here is declare it in the parent class:

class resources: def __init__(self): self.response = self.get_connect() def get_connect(self): return 1 + 2 

This way you do not need to define the __init__ method on every class, as it will be automatically inherited from the parent.

Regarding the second question, it really depends on the context, but you can use a strategy pattern in order to retrieve the class that you require to call. For this rename the method of get details into the same for each of the classes, as basically they're used for the same purpose, but changed on the context of the class implementation:

class Source1(resources): def get_details(self): print(self.response) class Source2(resources): def get_details(self): print(self.response) class Source3(resources): def get_details(self): print(self.response) classes = { "source_1": Source1, "source_2": Source2, "source_3": Source3 } source_class = classes["source_1"] source = source_class() source.get_details() 

Hope this helped!

Sign up to request clarification or add additional context in comments.

4 Comments

there is a reason to use get_connect as static because now you have an unused argument self
Well, it's an instance method, so it will use self, and usually the methods aren't that simplistic as return 1+2, methods may use other aspects/logic of the class. But in any case class methods can be used as well.
More likely, you'll still need to define __init__ in the child classes and call super().__init__ in each. Just as much repetition, but it's hard to tell if that's avoidable in this simplified example.
Not necessarily, if you don't have to override the __init__ for other implementations, you don't declare it in the child class

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.