Skip to main content
added 18 characters in body
Source Link
doca
  • 255
  • 1
  • 7

Say I have two object types of the same interface MyObj (I am using python in this project so I will use the same for the explanation)

class Foo(MyObj):
    a = [5, 10]

class Bar(MyObj):
    a = [[1, 2], [3, 4]]

Now I have this other class to do something with MyObj objects. Doing this is almost exactly the same for both Foo and Bar type of objects. However, an intermediate step of this process requires different attributes for Foo and Bar

class Thing:
    needed_func: Callable  # This function does something with the variable `a`

    def __init__(self, o: MyObj):
        """Initialization which is the same for Foo and Bar"""

    def do_the_thing():
        """steps to do the thing which is the same for both Foo and Bar"""
        # Here I need to use the `needed_func`

class ThingFoo(Thing):
    needed_func = func_for_foo # takes a 1D array as input

class ThingBar(Thing):
    needed_func = func_for_bar  # takes a 2D array as input

When I have the Thing.do_the_thing implementation, I only need the two line implementations of the ThingFoo and ThingBar implementations to work with Foo and Bar objects. Since I do not need to override the __init__ in the subclasses, both Thing's subclasses will accept any MyObj's subclasses which is bad design.

How should I structure my code such that ThingFoo and ThingBar only accept the respective objects?


TL;DR

Obviously I can implement init functions in the subclasses as follows. I am asking it there is a better way?

class ThingFoo(Thing):
    needed_func = func_for_foo # takes a 1D array as input

    def __init__(self, o: Foo):
        super().__init__(o)

class ThingBar(Thing):
    needed_func = func_for_bar  # takes a 2D array as input

    def __init__(self, o: Bar):
        super().__init__(o)

Say I have two object types of the same interface MyObj (I am using python in this project so I will use the same for the explanation)

class Foo(MyObj):
    a = [5, 10]

class Bar(MyObj):
    a = [[1, 2], [3, 4]]

Now I have this other class to do something with MyObj objects. Doing this is almost exactly the same for both Foo and Bar type of objects. However, an intermediate step of this process requires different attributes for Foo and Bar

class Thing:
    needed_func: Callable  # This function does something with the variable `a`

    def __init__(o: MyObj):
        """Initialization which is the same for Foo and Bar"""

    def do_the_thing():
        """steps to do the thing which is the same for both Foo and Bar"""
        # Here I need to use the `needed_func`

class ThingFoo(Thing):
    needed_func = func_for_foo # takes a 1D array as input

class ThingBar(Thing):
    needed_func = func_for_bar  # takes a 2D array as input

When I have the Thing.do_the_thing implementation, I only need the two line implementations of the ThingFoo and ThingBar implementations to work with Foo and Bar objects. Since I do not need to override the __init__ in the subclasses, both Thing's subclasses will accept any MyObj's subclasses which is bad design.

How should I structure my code such that ThingFoo and ThingBar only accept the respective objects?


TL;DR

Obviously I can implement init functions in the subclasses as follows. I am asking it there is a better way?

class ThingFoo(Thing):
    needed_func = func_for_foo # takes a 1D array as input

    def __init__(o: Foo):
        super().__init__(o)

class ThingBar(Thing):
    needed_func = func_for_bar  # takes a 2D array as input

    def __init__(o: Bar):
        super().__init__(o)

Say I have two object types of the same interface MyObj (I am using python in this project so I will use the same for the explanation)

class Foo(MyObj):
    a = [5, 10]

class Bar(MyObj):
    a = [[1, 2], [3, 4]]

Now I have this other class to do something with MyObj objects. Doing this is almost exactly the same for both Foo and Bar type of objects. However, an intermediate step of this process requires different attributes for Foo and Bar

class Thing:
    needed_func: Callable  # This function does something with the variable `a`

    def __init__(self, o: MyObj):
        """Initialization which is the same for Foo and Bar"""

    def do_the_thing():
        """steps to do the thing which is the same for both Foo and Bar"""
        # Here I need to use the `needed_func`

class ThingFoo(Thing):
    needed_func = func_for_foo # takes a 1D array as input

class ThingBar(Thing):
    needed_func = func_for_bar  # takes a 2D array as input

When I have the Thing.do_the_thing implementation, I only need the two line implementations of the ThingFoo and ThingBar implementations to work with Foo and Bar objects. Since I do not need to override the __init__ in the subclasses, both Thing's subclasses will accept any MyObj's subclasses which is bad design.

How should I structure my code such that ThingFoo and ThingBar only accept the respective objects?


TL;DR

Obviously I can implement init functions in the subclasses as follows. I am asking it there is a better way?

class ThingFoo(Thing):
    needed_func = func_for_foo # takes a 1D array as input

    def __init__(self, o: Foo):
        super().__init__(o)

class ThingBar(Thing):
    needed_func = func_for_bar  # takes a 2D array as input

    def __init__(self, o: Bar):
        super().__init__(o)
Source Link
doca
  • 255
  • 1
  • 7

Subclasses with same behaviour but different attributes for different inputs

Say I have two object types of the same interface MyObj (I am using python in this project so I will use the same for the explanation)

class Foo(MyObj):
    a = [5, 10]

class Bar(MyObj):
    a = [[1, 2], [3, 4]]

Now I have this other class to do something with MyObj objects. Doing this is almost exactly the same for both Foo and Bar type of objects. However, an intermediate step of this process requires different attributes for Foo and Bar

class Thing:
    needed_func: Callable  # This function does something with the variable `a`

    def __init__(o: MyObj):
        """Initialization which is the same for Foo and Bar"""

    def do_the_thing():
        """steps to do the thing which is the same for both Foo and Bar"""
        # Here I need to use the `needed_func`

class ThingFoo(Thing):
    needed_func = func_for_foo # takes a 1D array as input

class ThingBar(Thing):
    needed_func = func_for_bar  # takes a 2D array as input

When I have the Thing.do_the_thing implementation, I only need the two line implementations of the ThingFoo and ThingBar implementations to work with Foo and Bar objects. Since I do not need to override the __init__ in the subclasses, both Thing's subclasses will accept any MyObj's subclasses which is bad design.

How should I structure my code such that ThingFoo and ThingBar only accept the respective objects?


TL;DR

Obviously I can implement init functions in the subclasses as follows. I am asking it there is a better way?

class ThingFoo(Thing):
    needed_func = func_for_foo # takes a 1D array as input

    def __init__(o: Foo):
        super().__init__(o)

class ThingBar(Thing):
    needed_func = func_for_bar  # takes a 2D array as input

    def __init__(o: Bar):
        super().__init__(o)