Hello I'd like to use a class method that requires multiple arguments as well as an attribute from the class' __init__().
However, because I have to make this call several times, I'd like to avoid providing multiple arguments to each call. Therefore, I thought to make a child class which supplies the arguments such as below. However, I don't want to override the parent method in the child class.
Here are the two classes with an example call:
class Parent:
def __init__(self, parent_arg: dict) -> None:
self.parent_arg = parent_arg
def create_child(self, child_arg):
return Child(child_arg, self.parent_arg)
def method_a(self, method_arg: str, child_arg: str) -> dict:
self.method_b(method_arg, child_arg)
def method_b(self, method_arg: str, child_arg: str) -> str:
print(f"{self.parent_arg}, {child_arg}, and {method_arg}")
class Child(Parent):
def __init__(self, child_arg: str, parent_arg: dict) -> None:
super().__init__(parent_arg)
self.child_arg = child_arg
def method_a(self, method_arg: str = None) -> dict:
return super().method_a(method_arg, self.child_arg)
def method_b(self, method_arg: str) -> str:
return super().method_b(method_arg, self.child_arg)
if __name__ == "__main__":
parent_instance = Parent(parent_arg="Parent Argument")
child_instance = parent_instance.create_child(child_arg="Child Argument")
child_instance.method_a(method_arg="Method Argument")
Again, I'd like to be able to call the same method names from Parent and Child instances. However, I'd like the computation to be done in the parent class methods. Thank you so so much for any and all help.
Here's one solution without inheritance, however, I'm curious if there's a more elegant solution.
class Child:
def __init__(self, child_arg: str, parent_arg: dict) -> None:
self.parent = Parent(parent_arg)
self.child_arg = child_arg
def method_a(self, method_arg: str = None) -> dict:
return self.parent.method_a(method_arg, self.child_arg)
def method_b(self, method_arg: str) -> str:
return self.parent.method_b(method_arg, self.child_arg)
self.child_argso that I don't have to provide this argument to subsequent calls?if __name__ == "__main__", I'd like to be able to callchild_instance.method_a(method_arg="Method Argument")rather thanchild_instance.method_a(method_arg="Method Argument", child_arg="Child Argument")