Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inheritance vs Union
#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


Messages In This Thread
Inheritance vs Union - by gserranowong - Jun-29-2021, 05:03 PM
RE: Inheritance vs Union - by buran - Jun-29-2021, 06:08 PM
RE: Inheritance vs Union - by buran - Jun-29-2021, 06:10 PM
RE: Inheritance vs Union - by gserranowong - Jun-29-2021, 06:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ImportError: cannot import name 'Union' from '_ctypes' (unknown location) ciuffoly 15 10,719 Oct-09-2020, 06:58 AM
Last Post: ciuffoly
  Lists union and intersecion arbiel 5 2,970 Mar-28-2020, 05:57 AM
Last Post: buran
  Union of dictionaries (taking max value on collision) meee 5 3,905 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