1

I am trying to utilize Python classes for defining and organizing functions that will make Rest API calls. The functions are interrelated to some extend and hence I am trying to use Classes to organize them. The structure I have in mind is as below:

Main_Class
(holds the common variables and funcs. like say clean_dict())
|
|-- Sub_Class1 
|   (Modifies the URL to access some Service 1)
|       |-- Func1 (makes the actual API call and returns result)
|       |-- Func2
|-- Sub_Class2
|   (Modifies the URL to access some Service 2)
|       |-- Func1 (same name, but unrelated to above Func1)
|       |-- Func3

Currently I have written this as Nested Classes. From within the __init__ of Main class, I initialize the subclass objects too and pass some variables to it. This allows me to call the functions within subclass like below. This is something I want to do because its looks cleaner to me and provides someone who reads code later an idea where everything is coming from:

main = main_class(common_args)
var1 = main.subClass1.Func1(specific_args_for_this_func)
var2 = main.subClass2.Func1(specific_args_for_this_func)
var3 = main.subClass1.Func3(specific_args_for_this_func)

Issue is, I want to use the common clean_dict() function defined in Main class within the subclass functions. With Nested Classes, I understand this wouldn't be possible.

So, what is the best approach so that

  1. I can keep the structure of how I call the functions with dot notation and
  2. While being able to access some common function defined in a main/parent class?

From my understanding, if I do class inheritance instead, I would have to initialize each subclass separately and code would be like below:

subClass1 = Sub_Class1(common_args)
subClass2 = Sub_Class2(common_args)
var1 = subClass1.Func1(specific_args_for_this_func)
var2 = subClass2.Func1(specific_args_for_this_func)
var3 = subClass1.Func3(specific_args_for_this_func)

I guess it goes without saying that I am only an intermediate level programmer and hopefully answer to this wouldn't be too complex.

pallupz
  • 793
  • 3
  • 9
  • 25
  • 2
    I dont see any good reason to use nested classes here. It is generally not a common pattern in Python – juanpa.arrivillaga May 13 '20 at 07:54
  • Perhaps a question to be asked to https://softwareengineering.stackexchange.com/ ? – fralau May 13 '20 at 07:57
  • with inheritance, can I do the same thing while keeping end code the same? If yes, how? – pallupz May 13 '20 at 07:58
  • _“I would have to initialize each subclass separately”_ Things to initialize are _objects_ (or _class instances_), not classes. And I don’t find it efficient to keep broken code. Rewrite it as soon as you find it’s broken, you’d spend more time later by maintaining it otherwise. – Melebius May 13 '20 at 08:03
  • @Melebius with the accepted answer in that link, will I be able just make a ```Pet``` object and then call bark as ```Pet.Dog.Bark()```? – pallupz May 13 '20 at 08:06
  • Almost everything is possible. (I mean: I could imagine achieving it with inheritance but I think it’s not a good idea.) The question is whether there is any _advantage_ in doing it so. Don’t use programming language’s features just because they are available. Use them when they bring you some advantages in comparison to other options. – Melebius May 13 '20 at 08:10
  • If you want more specific answers, you should better explain what you are asking. I think you should elaborate on this part: _“Issue is, I want to use the common clean_dict() function defined in Main class within the subclass functions. With Nested Classes, I understand this wouldn't be possible.”_ Could you show this code? Perhaps there are options on how to do so but we cannot tell you without seeing the code. – Melebius May 13 '20 at 08:17

0 Answers0