I am going to inherit a class from a library but __init__ method of this class makes calls some functions from its internal modules. However, I don't want to use these functions, too. Bear in mind that, I don't want to change any code in the library. I think, the library has bad design, calling specific functions in your __init__ method does not make life easier for people who wants to use inheritance. If it's another method than __init__ I could override the method but I need to call super from my class' __init__ method.
Here is the some code to illustrate the situation. Let's start with the library:
# library: inside of module a
class BaseClass(object):
def __init__(self, *args, **kwargs):
# initialize params
from utils import some_func, some_dict
some_func(...)
for key in some_dict.keys():
# do stuff
# library: inside of module utils
some_dict = {...}
def some_func(args): # do stuff
Here is my code:
# inside of a one module.
from A import BaseClass
class SubClass(BaseClass):
def __init__(self, *args, **kwargs):
super(SubClass, self).__init__(*args, **kwargs)
# Also I have implementation for the dict and some_func.
# inside of module a (my code not the library)
some_dict = {...}
def some_func(args): # do stuff
So, the problem is that if I do the call of the BaseClass' __init__, it will use their own implementation in their module, not my implementation for some_dict and some_func in module a. I have some solutions but I'd like to understand what's the best practice.
Possible Solutions
Use __new__ method inside of the SubClass.
class SubClass(BaseClass):
# ...
def __new__(typ, ...):
import library.a
from a import some_dict, some_func
library.a.some_dict = some_dict # replace it with my dict.
library.a.some_func = some_func # replace it with my function.
The downside: What if I don't want to change library's implementation for another method. Solution: I may change this temporary. After initialization, I can revert these values back. Maybe. Another problem is: what if I need to change these variables way before?
Use my package's __init__.py to change these variables very beginning.
# inside of `__init__`.py of my package. Very beginning.
import library.a
from a import some_dict, some_func
library.a.some_dict = some_dict # replace it with my dict.
library.a.some_func = some_func # replace it with my function.
Is there any good practice for this problem? I think, for this case, I am okay to change the values of some_dict and some_func through the whole code execution, so which solution do you think can be better for which conditions? Am I wrong thinking that the BaseClass implementation is problematic since it's more complex than it should be (e.g., importing modules, calling functions from that internal modules)? I think it might have been better if they make these calls in a separate functions so that I could override it. Or, __init__ can take dict and a function argument regarding these, so that I can provide it in object creation.
What's the best practice for this situation?