Python Forum

Full Version: Inheritance vs Union
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
First of all, I realize inheritance and Unions are totally two different things. However, I would like to get your opinions on this issue I detail below:

Let's say we have this class.
class MyConfig():
   config_type : SomeEnum 
   retries: int
   context: Context
This Context depends on the value of config_type meaning the context is dependent on its value. Which implementation of Context is best?

class MySpecificContext:
   value: int
   name: str

class OtherUnrelatedContext:
   length:int
    position: int

Contex = Union[MySpecificContex,OtherUnrelatedContext]
or

class Context:
   pass

class MySpecificContext(Context):
   value: int
   name: str

class OtherUnrelatedContext(Context):
   length:int
    position: int
To be more specific. Let say you have a configuration class, this configuration will hold data that will be used to configure a process. This process will contain a few things that relate to all configurations, like retries value. However, it is expected that based on config_type there is an additional context that is required. This is highly related to the particular configuration type. Note that these classes only hold data and they are not expected to implement any behaviors in the future. However, it is expected that the SomeEnum enumeration will be extended and more "contexts" will be added at some point. Think of my config as immutable.
Not sure I fully understand what you are trying to do, but for me config_type is/should be attribute of Context (maybe context type is better name). and MyConfig class can have short-hand access to Context.config_type.
from enum import Enum

class ConfigType(Enum):
    Type1 = 'foo'
    Type2 = 'bar'

class Context:
    def __init__(self, config_type):
        self.config_type = config_type

    def __str__(self):
        return f'Context object of type {self.config_type}'

class MyConfig:
    def __init__(self, ctx, value):
        self.ctx = ctx
        self.some_config = value

    @property
    def config_type(self):
        return self.ctx.config_type

ctx = Context(ConfigType.Type1)
my_config = MyConfig(ctx, 1)


print(my_config.some_config)
print(my_config.ctx)
print(my_config.config_type)
Output:
1 Context object of type ConfigType.Type1 ConfigType.Type1
You can see there is no need of inheritance or union. for me MySpecificContext is instance of Context class, not separate class.
I see you have made some changes in your original post while I was answering, that make my answer somewhat irrelevant
(Jun-29-2021, 05:03 PM)gserranowong Wrote: [ -> ]First of all, I realize inheritance and Unions are totally two different things. However, I would like to get your opinions on this issue I detail below:

Let's say we have this class.
class MyConfig():
   config_type : SomeEnum 
   retries: int
   context: Context
This Context depends on the value of config_type meaning the context is dependent on its value. Which implementation of Context is best?

class MySpecificContext:
   value: int
   name: str

class OtherUnrelatedContext:
   length:int
    position: int

Contex = Union[MySpecificContex,OtherUnrelatedContext]
or

class Context:
   pass

class MySpecificContext(Context):
   value: int
   name: str

class OtherUnrelatedContext(Context):
   length:int
    position: int
To be more specific. Let say you have a configuration class, this configuration will hold data that will be used to configure a process. This process will contain a few things that relate to all configurations, like retries value. However, it is expected that based on config_type there is an additional context that is required. This is highly related to the particular configuration type. Note that these classes only hold data and they are not expected to implement any behaviors in the future. However, it is expected that the SomeEnum enumeration will be extended and more "contexts" will be added at some point. Think of my config as immutable.


Thanks, you are right. I provided more context. However, your comments are really well thought.