Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inheritance vs Union
#1
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.
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I see you have made some changes in your original post while I was answering, that make my answer somewhat irrelevant
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ImportError: cannot import name 'Union' from '_ctypes' (unknown location) ciuffoly 15 10,447 Oct-09-2020, 06:58 AM
Last Post: ciuffoly
  Lists union and intersecion arbiel 5 2,856 Mar-28-2020, 05:57 AM
Last Post: buran
  Union of dictionaries (taking max value on collision) meee 5 3,778 Jan-17-2018, 09:14 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020