2021-06-30

Union vs Inheritance in python implementation

Let's say we have this class.

class MyConfig():
   config_type : SomeEnum 
   retries: int
   context: Context

This Context depends on the value of type meaning the context is dependent on its value. Which implementation of Context is best? why one is better than the other.


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.



from Recent Questions - Stack Overflow https://ift.tt/3h1IUR0
https://ift.tt/eA8V8J

No comments:

Post a Comment