So let's consider the following classes. I have no control over classes A, B, and C and can't change the implementation.
class A(object):
def __new__(cls, root=None):
if root == "Some conditions":
return super(A, cls).__init__(B)
else:
return super(A, cls).__init__(C)
def __init__(self) -> None:
local_variable = self._join(self.root, 'x') # Need to modify this string to make it work with my problem. I believe this should be overridden in a child class.
self.x = self.foo(local_variable)
class B(A):
def __init__(self, root='local path to the downloaded file'):
self.root = root
self._join = os.path.join
super(self.__class__, self).__init__()
class C(A):
def __init__(self, root='address of the remote server to download the file'):
self.root = root
super(self.__class__, self).__init__()
What I'm trying to do is overriding the class A's __init__
method to change the local_variable
(x
is hard coded and needs to be modified) and consequently changing the self.x
.
What I have tried to do is:
class D(A):
def __new__(cls, root=None):
return super().__new__(self, root)
def __init__(self):
super(D, self).__init__()
# Not sure how to proceed and change the local_variable and self.x
I was reading this answer and was wondering if class A __new__
method will make the overriding of the __init__
method more complicated or not?
Note: I simplified the classes to make it easier to understand the problem.