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
- I can keep the structure of how I call the functions with dot notation and
- 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.